hdu 3499 Flight (最短路径)

Flight

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. 
 

 

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
zhouzeyong   |   We have carefully selected several similar problems for you:  3501 3502 3503 3504 3505 
 

 

题意:

    在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 }
View Code

 

   分层图思想:

  1 //6078MS    23744K    1906 B    C++ 
  2 /*
  3 
  4 转自:http://yomean.blog.163.com/blog/static/189420225201110282390985/
  5 
  6 一看就想到了分层图,不过如果用分层图,有点杀鸡用牛刀的感觉,因为只有两层。但我还是写了,最后AC了。不过网上很多人都是用建反两向边求解。
  7 而对于分层图求最短路径问题,我们要注意的是,层与层之间的连线都是单向的,而且是从下一层指向上一层,而我们求最短路径的时候,起点总是在下一层,而终点总是在上一层,所以我们可以将经过层与层之间的特殊边的数目控制在n - 1(n是层数)。
  8  
  9 
 10 */
 11 #include<iostream>
 12 #include<cstdio>
 13 #include<string>
 14 #include<queue>
 15 #include<map>
 16 #include<vector>
 17 #define N 100005
 18 #define inf (_I64_MAX)/2
 19 using namespace std;
 20 int n,m;
 21 int head[2*N],vis[2*N];
 22 int now,index,k;
 23 __int64 dis[2*N];
 24 char name[N][10];
 25 map<string,int>M;
 26 struct node{
 27     int v,w,next;
 28 }edge[15*N];
 29 void addedge(int u,int v,int w)
 30 {
 31     edge[index].v=v;
 32     edge[index].w=w; 
 33     edge[index].next=head[u];
 34     head[u]=index++;
 35 }    
 36 struct cmp{
 37     bool operator()(int a,int b){
 38          return dis[a]>dis[b];
 39     }
 40 };
 41 priority_queue<int,vector<int>,cmp>Q;
 42 void init()
 43 {
 44     while(!Q.empty()) Q.pop();
 45     M.erase(M.begin(),M.end());
 46     for(int i=0;i<2*n;i++){
 47         vis[i]=false;
 48         head[i]=-1;
 49     }
 50     now=1;
 51     index=0;
 52 }
 53 void dij(int s,int e)
 54 {
 55     for(int i=0;i<=2*n;i++){
 56        dis[i]=inf;vis[i]=false;
 57     }
 58     dis[s]=0;
 59     vis[s]=true;
 60     Q.push(s);
 61     while(!Q.empty()){
 62         int u=Q.top();
 63         Q.pop();
 64         if(u==e){
 65             printf("%I64d\n",dis[u]);
 66             return;
 67         }
 68         for(int i=head[u];i!=-1;i=edge[i].next){
 69             int v=edge[i].v;
 70             int w=edge[i].w;
 71             if(!vis[v] && dis[v]>dis[u]+w){
 72                 dis[v]=dis[u]+w;
 73                 Q.push(v);
 74             }
 75         }
 76     } 
 77 }
 78 int main(void)
 79 {
 80     string a,b;
 81     int x,y,c;
 82     while(cin>>n>>m)
 83     {
 84         init();
 85         for(int i=0;i<m;i++){
 86             cin>>a>>b>>c;
 87             if(M.find(a)==M.end()) M[a]=now++;
 88             if(M.find(b)==M.end()) M[b]=now++;
 89             addedge(M[a],M[b],c);
 90             addedge(M[a]+n,M[b]+n,c);
 91             addedge(M[a]+n,M[b],c/2);
 92         }
 93         cin>>a>>b;
 94         __int64 ans=inf;
 95         if(M.find(a)==M.end() || M.find(b)==M.end()){
 96             puts("-1");continue;
 97         }
 98         else dij(M[a]+n,M[b]);
 99     }
100     return 0;
101 }
View Code

 

 找了一个G++能过的,不过没自己实现,略感无语

  1 //3765MS    28756K    2269 B    G++ 
  2 //转载: http://blog.csdn.net/shoutmon/article/details/8583984 
  3 /*
  4    
  5     思路:
  6 
  7 1.先正向建图,以a为源点跑Dijkstra
  8 
  9 2.再反向建图,以b为源点跑Dijkstra
 10 
 11 3.枚举边(作为花费变为一半的边),从a到这条边的起点u使用正向建图的结果,从这条边的终点v使用反向建图的结果,然后再加上这条边边权的一半,就得到这条边花费变为一半时候的总花费。
 12 
 13 4.将枚举结果取最小值即为最小花费
 14 
 15 5.注意输入是字符串,可以用map
 16 
 17 
 18 */ 
 19 #include<iostream>
 20 #include<cstdio>
 21 #include<cstring>
 22 #include<string>
 23 #include<algorithm>
 24 #include<queue>
 25 #include<map>
 26 
 27 using namespace std;
 28 
 29 typedef __int64 ll;
 30 
 31 const int N=100010;
 32 const int M=500010;
 33 const ll inf=1LL<<60;
 34 
 35 struct node
 36 {
 37     int to;
 38     ll dis;
 39     node *next;
 40 }E[M<<1],*G1[N],*G2[N],*head;
 41 
 42 int n,m,num;
 43 ll d1[N],d2[N];
 44 bool inq[N];
 45 map<string,int> dict;
 46 
 47 inline void add(int a,int b,ll c,node *G[])
 48 {
 49     head->to=b;
 50     head->dis=c;
 51     head->next=G[a];
 52     G[a]=head++;
 53 }
 54 
 55 inline int change(char *s)
 56 {
 57     if(dict.count(s)) return dict[s];
 58     else return dict[s]=num++;
 59 }
 60 
 61 void SPFA(int s,ll d[],node *G[])
 62 {
 63 
 64     deque<int> Q;
 65     Q.push_back(s);
 66     memset(inq,false,sizeof(inq));
 67     fill(d,d+N,inf);
 68     d[s]=0;
 69     int to;
 70     ll dis;
 71     while(!Q.empty())
 72     {
 73         int u=Q.front();
 74         Q.pop_front();
 75         inq[u]=false;
 76         for(node *p=G[u];p;p=p->next)
 77         {
 78             to=p->to;
 79             dis=p->dis;
 80             if(d[to]>d[u]+dis)
 81             {
 82                 d[to]=d[u]+dis;
 83                 if(!Q.empty())
 84                 {
 85                     if(d[to]>d[Q.front()]) Q.push_back(to);
 86                     else Q.push_front(to);
 87                 }
 88                 else Q.push_back(to);
 89             }
 90         }
 91     }
 92 }
 93 
 94 int main()
 95 {
 96     char s1[20],s2[20];
 97     while(~scanf("%d%d",&n,&m))
 98     {
 99         num=0;
100         dict.clear();
101         memset(G1,NULL,sizeof(G1));
102         memset(G2,NULL,sizeof(G2));
103         head=E;
104         int s,t;
105         ll dis;
106         for(int i=0;i<m;i++)
107         {
108             scanf("%s %s %I64d",s1,s2,&dis);
109             s=change(s1),t=change(s2);
110             add(s,t,dis,G1);
111             add(t,s,dis,G2);
112         }
113         scanf("%s %s",s1,s2);
114         s=dict[s1],t=dict[s2];
115 
116         SPFA(s,d1,G1);
117         SPFA(t,d2,G2);
118 
119 
120         ll ans=inf;
121         for(int i=0;i<n;i++)
122         {
123             for(node *p=G1[i];p;p=p->next)
124             {
125                 int j=p->to;
126                 if(d1[i]<inf && d2[j]<inf) ans=min(ans,d1[i]+d2[j]+(p->dis)/2);
127             }
128         }
129 
130         if(ans==inf) printf("-1\n");
131         else printf("%I64d\n",ans);
132     }
133     return 0;
134 }
View Code

 

 

posted @ 2013-11-19 11:03  heaventouch  阅读(417)  评论(0编辑  收藏  举报