Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2014 Accepted Submission(s): 428
Problem Description
Recently, Shua Shua had a big quarrel with his GF. He is so upset that he decides to take a trip to some other city to avoid meeting her. He will travel only by air and he can go to any city if there exists a flight and it can help him reduce the total cost to the destination. There's a problem here: Shua Shua has a special credit card which can reduce half the price of a ticket ( i.e. 100 becomes 50, 99 becomes 49. The original and reduced price are both integers. ). But he can only use it once. He has no idea which flight he should choose to use the card to make the total cost least. Can you help him?
Input
There are no more than 10 test cases. Subsequent test cases are separated by a blank line.
The first line of each test case contains two integers N and M ( 2 <= N <= 100,000
0 <= M <= 500,000 ), representing the number of cities and flights. Each of the following M lines contains "X Y D" representing a flight from city X to city Y with ticket price D ( 1 <= D <= 100,000 ). Notice that not all of the cities will appear in the list! The last line contains "S E" representing the start and end city. X, Y, S, E are all strings consisting of at most 10 alphanumeric characters.
The first line of each test case contains two integers N and M ( 2 <= N <= 100,000
0 <= M <= 500,000 ), representing the number of cities and flights. Each of the following M lines contains "X Y D" representing a flight from city X to city Y with ticket price D ( 1 <= D <= 100,000 ). Notice that not all of the cities will appear in the list! The last line contains "S E" representing the start and end city. X, Y, S, E are all strings consisting of at most 10 alphanumeric characters.
Output
One line for each test case the least money Shua Shua have to pay. If it's impossible for him to finish the trip, just output -1.
Sample Input
4 4
Harbin Beijing 500
Harbin Shanghai 1000
Beijing Chengdu 600
Shanghai Chengdu 400
Harbin Chengdu
4 0
Harbin Chengdu
Sample Output
800
-1
Hint
In the first sample, Shua Shua should use the card on the flight from
Beijing to Chengdu, making the route Harbin->Beijing->Chengdu have the
least total cost 800. In the second sample, there's no way for him to get to
Chengdu from Harbin, so -1 is needed.
Author
Edelweiss
Source
Recommend
题意:
在N个点,M条带权边的图上,查询从点s到点e的最短路径,不过,可以有一次机会可以把一条边的权值变成原来的一半。
小菜代码(双向求解,G++不能过...):
1 //6890MS 41488K 2295 B C++ 2 /* 3 建图双向求解 4 */ 5 #include<iostream> 6 #include<queue> 7 #include<vector> 8 #include<map> 9 #include<string> 10 #define N 100005 11 using namespace std; 12 struct node{ 13 __int64 v,w; 14 node(__int64 a,__int64 b){ 15 v=a;w=b; 16 } 17 }; 18 const __int64 inf=(_I64_MAX)/2; 19 __int64 dis[2][N]; 20 bool vis[N]; 21 __int64 from[5*N],to[5*N],weight[5*N]; //记录边信息 22 vector<node>V[2][N]; 23 map<string,__int64>M; 24 __int64 n,m,sign; 25 __int64 start,end; 26 void spfa() 27 { 28 __int64 s; 29 if(sign==0) s=start; 30 else s=end; 31 for(int i=0;i<=n;i++) 32 dis[sign][i]=inf; 33 memset(vis,false,sizeof(vis)); 34 queue<int>Q; 35 Q.push(s); 36 dis[sign][s]=0; 37 while(!Q.empty()){ 38 int u=Q.front(); 39 Q.pop(); 40 vis[u]=false; 41 int n0=V[sign][u].size(); 42 for(int i=0;i<n0;i++){ 43 __int64 v=V[sign][u][i].v; 44 __int64 w=V[sign][u][i].w; 45 if(dis[sign][v]>dis[sign][u]+w){ 46 dis[sign][v]=dis[sign][u]+w; 47 if(!vis[v]){ 48 Q.push(v); 49 vis[v]=true; 50 } 51 } 52 } 53 } 54 } 55 int main(void) 56 { 57 string a,b; 58 __int64 c; 59 while(cin>>n>>m) 60 { 61 M.clear(); 62 for(int i=0;i<=n;i++){ 63 V[0][i].clear(); 64 V[1][i].clear(); 65 } 66 int id=0; 67 for(int i=0;i<m;i++){ 68 cin>>a>>b>>c; 69 if(M[a]==0) M[a]=++id; 70 if(M[b]==0) M[b]=++id; 71 V[0][M[a]].push_back(node(M[b],c)); 72 V[1][M[b]].push_back(node(M[a],c)); 73 from[i]=M[a]; 74 to[i]=M[b]; 75 weight[i]=c; 76 } 77 cin>>a>>b; 78 if(M[a]==0 || M[b]==0){ 79 puts("-1");continue; 80 } 81 start=M[a]; 82 end=M[b]; 83 84 if(start==end){ 85 puts("0");continue; 86 } 87 88 sign=0; 89 spfa(); 90 if(dis[sign][end]==inf){ 91 puts("-1");continue; 92 } 93 94 sign=1; 95 spfa(); 96 97 __int64 ans=inf; 98 for(int i=0;i<m;i++){ 99 ans=min(ans,dis[0][from[i]]+dis[1][to[i]]+weight[i]/2); 100 } 101 if(ans==inf) puts("-1"); 102 else printf("%I64d\n",ans); 103 } 104 return 0; 105 }