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
题目来源
参考博客: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的更多相关文章
- Our Journey of Dalian Ends && Our Journey of Xian Ends 最小费用最大流
2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛Our Journey of Dalian Ends 题意:要求先从大连到上海,再从上海打西安,中途会经过其他城市,每个城市只能去一次,出一次, ...
- 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 ...
- Our Journey of Dalian Ends 乌鲁木齐网络赛 最小费用最大流
Life is a journey, and the road we travel has twists and turns, which sometimes lead us to unexpecte ...
- 2017乌鲁木齐网络赛 J题 Our Journey of Dalian Ends ( 最小费用最大流 )
题目链接 题意 : 给出一副图,大连是起点,终点是西安,要求你求出从起点到终点且经过中转点上海的最小花费是多少? 分析 : 最短路是最小费用最大流的一个特例,所以有些包含中转限制或者经过点次数有限制的 ...
- 2017乌鲁木齐网络赛 j 题
题目连接 : https://nanti.jisuanke.com/t/A1256 Life is a journey, and the road we travel has twists and t ...
- CF721C. Journey[DP DAG]
C. Journey time limit per test 3 seconds memory limit per test 256 megabytes input standard input ou ...
- POJ2488A Knight's Journey[DFS]
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 41936 Accepted: 14 ...
- 关于cout<<ends你不知道的那些事
关于ends是C++中比较基础的一个东西,但是可能不是每个人都能够清楚的理解这是个什么东西,我就经历了这么一个过程,写出来让大家看看,有什么理解的不对的地方欢迎拍砖. 今天以前我对ends的理解是:输 ...
- CF #374 (Div. 2) C. Journey dp
1.CF #374 (Div. 2) C. Journey 2.总结:好题,这一道题,WA,MLE,TLE,RE,各种姿势都来了一遍.. 3.题意:有向无环图,找出第1个点到第n个点的一条路径 ...
随机推荐
- MySQL excel导入
说明: 1 因在测试发现如果用SQLyog导入数据需要下载excel驱动,因而选择Navicat 2 之前选择excel文件为xlsx 发现Navicat识别不了,因而转存为xls文件,测试OK 1 ...
- unity3d的碰撞检测及trigger
A.基本概念 要产生碰撞必须为游戏对象添加刚体(Rigidbody)和碰撞器,刚体可以让物体在物理影响下运动.碰撞体是物理组件的一类,它要与刚体一起添加到游戏对象上才能触发碰撞.如果两个刚体相互撞在一 ...
- K-means算法(理论+opencv实现)
写在前面:之前想分类图像的时候有看过k-means算法,当时一知半解的去使用,不懂原理不懂使用规则...显然最后失败了,然后看了<机器学习>这本书对k-means算法有了理论的认识,现在通 ...
- json , 正则
json: import json user = { 'dsada': 'whichT','a':True,'b':None } a=json.dumps(user,indent=3,sort_key ...
- CentOS Apache配置详解
要想在linux上实现网页服务器(www)需要Apache这个服务器软件,不过Apache仅能提供最基本的静态网站数据而已,想要实现动态网站的话,最好还是要PHP与MySQL的支持,所以下面我们将会以 ...
- 28. 表单css样式定义格式
form>table>tbody>tr>td{padding:5px;font-size:14px;font-family:"Microsoft YaHei" ...
- leetcode100
/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...
- jd-eclipse反编译插件的在线安装和使用
jd-eclipse反编译插件的在线安装和使用 JD-Eclipse是一个Eclipse平台的插件.它允许您调试所有的Java源代码,有了它,以后调试的时候ctrl键就可以一键到底啦.下面简单说说ec ...
- 简单的socket_server 和 socket_client(实现文件的上传功能)
socket_server 客户端程序 import socket, os, json class Ftcplient(object): def __init__(self): "" ...
- S+ hidden tray with window start
官方论坛上有个帖子给出了答案: I forgot that this is supported in the code, but it requires a little editing of the ...