Our Journey of Xian Ends
https://nanti.jisuanke.com/t/18521
Life is a journey, and the road we travel has twists and turns, which sometimes lead us to unexpected places and unexpected people. Now our journey of Xian ends. To be carefully considered are the following questions.
A few months later in Qingdao, an essential ACM competition had been scheduled. But before the competition, we need to attend a wedding in Shanghai. And after the competition, we will leave the country from Shanghai, so Pudong International Airport (Pudong in short) is the end of our tour.
Here we have some vital information and missions we have to accomplish.
We have a VIP card of CNAC. For each airport we can enjoy the special VIP services in the departure floor and the arrival floor once respectively. For the pleasure of traveling, it is intolerant without VIP services. That is say that for each airport we can leave from it only once, but without regard to the last flight leaving the country from Pudong, Shanghai. Meanwhile, for each airport we can arrive at it only once.
All as we know, Shanghai has two airports, Hongqiao Airport (Hongqiao in short) and Pudong. Arriving at one and then leaving from another one is a spurned thing. But fortunately there is a nice and evil compensation service. Having a pair of transfer records between Hongqiao and Pudong in both directions, we can obtain a sensible compensation. Actually, we only consider planes in our tour, with the only exception in Shanghai. The exception is that we can arrive and leave Shanghai at different airports. However, if we decide so the compensation described above is necessary. Before the end of our tour, we will pass through Shanghai twice, once for the wedding and another time for the final departure. If we want to obtain the compensation, in the first time we must transfer from Pudong to Hongqiao, and in the second time we will transfer from Hongqiao to Pudong.
Similar transfers between airports in other city are not allowed. If we arrived at a city, we would not go to an airport in an adjacent city by car, bus or interurban railway as well.
Now, all available flights between airports are known. We have plenty of time yet. So we do not have any restriction about the number of times. What we require is the smallest total cost of flights throughout the whole tour.
Here we go.
Input
There are several test cases. The first line of input contains an integer t (1 ≤ t ≤ 160) which is the total number of test cases. For each test case, the first line contains an integer m (m ≤ 10000) which is the number of known flights. Each of the following m lines describes a flight which contains two string indicating the names of two airports and an integer between 1 and 255 indicating the cost. The flight connects two given airports and it is bidirectional. The name of each airport is an non-empty string with English letters that are no longer than 10. We use “Xian” to present the only airport in Xian, and use “Qingdao” to present the only airport in Qingdao. The airports in Shanghai are described as “Hongqiao” and “Pudong” respectively.
Output
For each test case, output the smallest total cost, or output −1 if it is impossible.
样例输入
3 4 Xian Hongqiao 3 Xian Pudong 4 Qingdao Hongqiao 4 Qingdao Pudong 3 4 Xian Hongqiao 4 Xian Pudong 3 Qingdao Hongqiao 3 Qingdao Pudong 4 6 Xian Hongqiao 4 Xian Pudong 3 Qingdao Hongqiao 3 Qingdao Pudong 4 Qingdao Xuzhou 1 Xuzhou Hongqiao 1
样例输出
10 9 8
题目来源
1 #include<iostream> 2 #include<algorithm> 3 #include<queue> 4 #include<cstring> 5 #include<map> 6 using namespace std; 7 8 const int INF=0x3f3f3f3f; 9 const int N=500005; 10 const int M=500005; 11 int top; 12 int dist[N],pre[N]; 13 bool vis[N]; 14 int c[N]; 15 int maxflow; 16 17 struct Vertex{ 18 int first; 19 }V[N]; 20 struct Edge{ 21 int v,next; 22 int cap,flow,cost; 23 }E[M]; 24 25 void init(){ 26 memset(V,-1,sizeof(V)); 27 top=0; 28 maxflow=0; 29 } 30 31 void add_edge(int u,int v,int c,int cost){ 32 E[top].v=v; 33 E[top].cap=c; 34 E[top].flow=0; 35 E[top].cost=cost; 36 E[top].next=V[u].first; 37 V[u].first=top++; 38 } 39 40 void add(int u,int v,int c,int cost){ 41 add_edge(u,v,c,cost); 42 add_edge(v,u,0,-cost); 43 } 44 45 bool SPFA(int s,int t,int n){ 46 int i,u,v; 47 queue<int>qu; 48 memset(vis,false,sizeof(vis)); 49 memset(c,0,sizeof(c)); 50 memset(pre,-1,sizeof(pre)); 51 for(i=1;i<=n;i++){ 52 dist[i]=INF; 53 } 54 vis[s]=true; 55 c[s]++; 56 dist[s]=0; 57 qu.push(s); 58 while(!qu.empty()){ 59 u=qu.front(); 60 qu.pop(); 61 vis[u]=false; 62 for(i=V[u].first;~i;i=E[i].next){ 63 v=E[i].v; 64 if(E[i].cap>E[i].flow&&dist[v]>dist[u]+E[i].cost){ 65 dist[v]=dist[u]+E[i].cost; 66 pre[v]=i; 67 if(!vis[v]){ 68 c[v]++; 69 qu.push(v); 70 vis[v]=true; 71 if(c[v]>n){ 72 return false; 73 } 74 } 75 } 76 } 77 } 78 if(dist[t]==INF){ 79 return false; 80 } 81 return true; 82 } 83 84 int MCMF(int s,int t,int n){ 85 int d; 86 int i,mincost; 87 mincost=0; 88 while(SPFA(s,t,n)){ 89 d=INF; 90 for(i=pre[t];~i;i=pre[E[i^1].v]){ 91 d=min(d,E[i].cap-E[i].flow); 92 } 93 maxflow+=d; 94 for(i=pre[t];~i;i=pre[E[i^1].v]){ 95 E[i].flow+=d; 96 E[i^1].flow-=d; 97 } 98 mincost+=dist[t]*d; 99 } 100 return mincost; 101 } 102 103 int main(){ 104 int n,m; 105 int v,u,w,c; 106 int s,t; 107 int T; 108 cin>>T; 109 while(T--){ 110 cin>>n; 111 init(); 112 int co=1; 113 map<string,int>mp; 114 string str1,str2; 115 for(int i=1;i<=n;i++){ 116 cin>>str1>>str2>>w; 117 //cin>>u>>v>>w>>c; 118 if(!mp[str1]) mp[str1]=co++; 119 if(!mp[str2]) mp[str2]=co++; 120 add(mp[str1],mp[str2]+n+n,INF,w); 121 add(mp[str2],mp[str1]+n+n,INF,w); 122 } 123 map<string,int>::iterator it; 124 for(it=mp.begin();it!=mp.end();it++){ 125 if(it->first=="Xian"){ 126 add(0,it->second+2*n,1,0); 127 128 add(it->second+2*n,it->second,1,0); 129 } 130 else if(it->first=="Qingdao"){ 131 add(0,it->second+2*n,2,0); 132 133 add(it->second+2*n,it->second,2,0); 134 } 135 else if(it->first=="Hongqiao"){ 136 add(it->second,4*n+1,2,0); 137 138 add(it->second+2*n,it->second,2,0); 139 } 140 else if(it->first=="Pudong"){ 141 add(it->second,4*n+1,1,0); 142 143 add(it->second+2*n,it->second,1,0); 144 } 145 else{ 146 add(it->second+2*n,it->second,1,0); 147 } 148 } 149 s=0,t=4*n+1; 150 int ans=MCMF(s,t,4*n+2); 151 if(maxflow==3) cout<<ans<<endl; 152 else cout<<-1<<endl; 153 } 154 }