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;
}
}

Our Journey of Xian Ends的更多相关文章

  1. Our Journey of Dalian Ends && Our Journey of Xian Ends 最小费用最大流

    2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛Our Journey of Dalian Ends 题意:要求先从大连到上海,再从上海打西安,中途会经过其他城市,每个城市只能去一次,出一次, ...

  2. 2017 乌鲁木齐赛区网络赛 J Our Journey of Dalian Ends 费用流

    题目描述: Life is a journey, and the road we travel has twists and turns, which sometimes lead us to une ...

  3. Our Journey of Dalian Ends 乌鲁木齐网络赛 最小费用最大流

    Life is a journey, and the road we travel has twists and turns, which sometimes lead us to unexpecte ...

  4. 2017乌鲁木齐网络赛 J题 Our Journey of Dalian Ends ( 最小费用最大流 )

    题目链接 题意 : 给出一副图,大连是起点,终点是西安,要求你求出从起点到终点且经过中转点上海的最小花费是多少? 分析 : 最短路是最小费用最大流的一个特例,所以有些包含中转限制或者经过点次数有限制的 ...

  5. 2017乌鲁木齐网络赛 j 题

    题目连接 : https://nanti.jisuanke.com/t/A1256 Life is a journey, and the road we travel has twists and t ...

  6. CF721C. Journey[DP DAG]

    C. Journey time limit per test 3 seconds memory limit per test 256 megabytes input standard input ou ...

  7. POJ2488A Knight's Journey[DFS]

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 41936   Accepted: 14 ...

  8. 关于cout<<ends你不知道的那些事

    关于ends是C++中比较基础的一个东西,但是可能不是每个人都能够清楚的理解这是个什么东西,我就经历了这么一个过程,写出来让大家看看,有什么理解的不对的地方欢迎拍砖. 今天以前我对ends的理解是:输 ...

  9. CF #374 (Div. 2) C. Journey dp

    1.CF #374 (Div. 2)    C.  Journey 2.总结:好题,这一道题,WA,MLE,TLE,RE,各种姿势都来了一遍.. 3.题意:有向无环图,找出第1个点到第n个点的一条路径 ...

随机推荐

  1. js判断是android访问还是ios访问

    原文地址:http://blog.csdn.net/wy978651775/article/details/9014039 该博主也是转载的,但是没有标明出处. 判断原理: JavaScript是前端 ...

  2. 关于Oracle与MySQL的使用总结

    平时使用的比较多的数据库管理系统就是Oracle和MySQL,我在这里记录下使用过程中的遇到的问题以及解决方案,以备不时之需 Oracle 关于表空间 Oracle创建数据的代价还是比较大的,所以使用 ...

  3. iOS 一些琐碎的知识点

    1. Xcode左边导航栏中,类文件后面的标记"A""M""?"符号的含义 M = Locally modified 文件已被修改 U = ...

  4. Samba 简介

    SMB 代表的是服务器消息块 (Server Message Block),它是用于在 Windows 上共享文件的协议的原始名称. CIFS 代表公共 Internet 文件系统 (Common I ...

  5. 属性,类方法@classmethod

    # 属性的初识# class Person:## def __init__(self,name,hight,weight):# self.name = name# self.__hight = hig ...

  6. Sklearn数据集与机器学习

    sklearn数据集与机器学习组成 机器学习组成:模型.策略.优化 <统计机器学习>中指出:机器学习=模型+策略+算法.其实机器学习可以表示为:Learning= Representati ...

  7. kvm安装及使用

    ****centos7安装及使用kvm: http://blog.csdn.net/github_27924183/article/details/76914322?locationNum=5& ...

  8. apache配置修改

    1.如何设置请求等待时间   在httpd.conf里面设置:   TimeOut n   其中n为整数,单位是秒.     2. 3.如何使得apache监听在特定的端口   修改httpd.con ...

  9. delphi ios grid BindSourceDB bug

    BindSourceDB4.DataSet :=nil; BindSourceDB4.DataSet :=FDMemTable1; grid绑定后显示数据正常,第二次赋值BindSourceDB4.D ...

  10. Activity服务类-4 HistoryService服务类

    一共个方法15个方法 用于查询历史工作流信息1.创建查询(7个方法)//创建一个新的编程查询来搜索{@link HistoricProcessInstance}.HistoricProcessInst ...