Our Journey of Xian Ends

Our Journey of Xian Ends

https://nanti.jisuanke.com/t/18521

  • 262144K
 

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

题目来源

ACM-ICPC 2017 Asia Qingdao

参考博客:https://blog.csdn.net/wangshuhe963/article/details/78516821

题意:有三个城市:西安,上海,青岛,现在要求在这三个城市之间往返,要求先从西安到上海,再从上海到青岛,再从青岛到上海,问最小花费,每个城市只有一个机场因此只能走一次,但上海有两个机场:虹桥机场和浦东机场,又因为上海有两个机场,所以这里有一个限定条件:只允许从浦东机场前往虹桥机场,也就是说如果从西安来的某条路线到达浦东机场,那么将有两种选择,一种去虹桥机场出发,另一种从浦东出发,但是如果西安的某条路线到达虹桥机场,那么将只有一种选择,从虹桥机场出发;

思路:裸的最小费用流问题,但是建图是个难点,其实思路还是不难得,首先因为每个城市只能走一次,所以拆点,把每个城市拆成u和u',然后连u->u'费用为0,流量为1的边,但是这里要注意,青岛这个点和虹桥这个点要连流量为2,费用为0的边,因为虹桥和青岛这里都是两次经过的点,仔细一想就不难想到这样的可行性,然后对于图中给出的边u->v,连u'->v和v'->u的费用为边权流量为INF的有向边,之后建一个源点连虹桥和浦东,费用均为0,但是连虹桥的流量为2,浦东的流量为1,再建一个汇点,连西安和青岛,同样费用均为0,但是连西安的流量为1,青岛的为2,好了,图建好了,直接跑一边费用流,如果最大流为3,就输出,否则就是-1;

 #include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
#include<map>
using namespace std; const int INF=0x3f3f3f3f;
const int N=;
const int M=;
int top;
int dist[N],pre[N];
bool vis[N];
int c[N];
int maxflow; struct Vertex{
int first;
}V[N];
struct Edge{
int v,next;
int cap,flow,cost;
}E[M]; void init(){
memset(V,-,sizeof(V));
top=;
maxflow=;
} void add_edge(int u,int v,int c,int cost){
E[top].v=v;
E[top].cap=c;
E[top].flow=;
E[top].cost=cost;
E[top].next=V[u].first;
V[u].first=top++;
} void add(int u,int v,int c,int cost){
add_edge(u,v,c,cost);
add_edge(v,u,,-cost);
} bool SPFA(int s,int t,int n){
int i,u,v;
queue<int>qu;
memset(vis,false,sizeof(vis));
memset(c,,sizeof(c));
memset(pre,-,sizeof(pre));
for(i=;i<=n;i++){
dist[i]=INF;
}
vis[s]=true;
c[s]++;
dist[s]=;
qu.push(s);
while(!qu.empty()){
u=qu.front();
qu.pop();
vis[u]=false;
for(i=V[u].first;~i;i=E[i].next){
v=E[i].v;
if(E[i].cap>E[i].flow&&dist[v]>dist[u]+E[i].cost){
dist[v]=dist[u]+E[i].cost;
pre[v]=i;
if(!vis[v]){
c[v]++;
qu.push(v);
vis[v]=true;
if(c[v]>n){
return false;
}
}
}
}
}
if(dist[t]==INF){
return false;
}
return true;
} int MCMF(int s,int t,int n){
int d;
int i,mincost;
mincost=;
while(SPFA(s,t,n)){
d=INF;
for(i=pre[t];~i;i=pre[E[i^].v]){
d=min(d,E[i].cap-E[i].flow);
}
maxflow+=d;
for(i=pre[t];~i;i=pre[E[i^].v]){
E[i].flow+=d;
E[i^].flow-=d;
}
mincost+=dist[t]*d;
}
return mincost;
} int main(){
int n,m;
int v,u,w,c;
int s,t;
int T;
cin>>T;
while(T--){
cin>>n;
init();
int co=;
map<string,int>mp;
string str1,str2;
for(int i=;i<=n;i++){
cin>>str1>>str2>>w;
if(!mp[str1]) mp[str1]=co++;
if(!mp[str2]) mp[str2]=co++;
add(mp[str1]+n+n,mp[str2],INF,w);
add(mp[str2]+n+n,mp[str1],INF,w);
}
s=,t=*n+;
map<string,int>::iterator it;
for(it=mp.begin();it!=mp.end();it++){
if(it->first=="Xian"){
add(s,it->second,,);
add(it->second,it->second+*n,,);
}
else if(it->first=="Qingdao"){
add(s,it->second,,);
add(it->second,it->second+*n,,);
}
else if(it->first=="Hongqiao"){
add(it->second+*n,t,,);
add(it->second,it->second+*n,,);
}
else if(it->first=="Pudong"){
add(it->second+*n,t,,);
add(it->second,it->second+*n,,);
}
else{
add(it->second,it->second+*n,,);
}
}
int ans=MCMF(s,t,*n+);
if(maxflow==) cout<<ans<<endl;
else cout<<-<<endl;
}
}
上一篇:gulp安装及使用流程


下一篇:Linux下 查找大文件