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个点的一条路径 ...
随机推荐
- 在VM克隆CENTOS以后,网卡的处理过程
会发现克隆CENTOS以后,网卡eth0无法启动.处理步骤如下:1. vi /etc/sysconfig/network-scripts/ifcfg-eth0删除HWADDR地址那行删除UUID的那行 ...
- Javascript中Closure及其相关概念
我相信学过Javascript这门语言的程序员应该都对Closure这个概念有所了解,然而网上以及各种Javascript书籍里面对Closure这个概念的定义有各种说法.我本人觉得很多地方对Clos ...
- oracle11g的dmp文件导入oracle10g时报错:头部验证失败
因为本机安装的10g版本的Oracle,需要导入一个11g版本导出的dmp文件,Oracle数据库版本之间存在兼容的问题,低版本的库不能导入高版本的dmp文件,在CMD中导入dmp文件总是出现如下错误 ...
- Oracle 统计量NO_INVALIDATE参数配置(下)
转载:http://blog.itpub.net/17203031/viewspace-1067620/ 本篇我们继续讨论NO_INVALIDATE参数. 从上篇(http://blog.itpub. ...
- MySQL 安装方法
所有平台的Mysql下载地址为: MySQL 下载. 挑选你需要的 MySQL Community Server 版本及对应的平台. Linux/UNIX上安装Mysql Linux平台上推荐使用RP ...
- NHibernate.Cfg.HibernateConfigException
在用NHibernate 框架做web 项目时,当项目被成功编译后,按F5 启动调试时,一开始就出现这个错误,刚开始就很郁闷,到底出在哪里?连自己都 不知道,在网上搜来搜去,找了很多的资料终于弄明白了 ...
- mac 安装 nginx
我是用root用户装的 1.先安装PCRE库 可以在这里下载最新版,我这里使用的是8.33的版本然后在终端执行下面的命令. cd ~/Download tar xvzf pcre-8.33.tar.g ...
- selenium+python自动化96-执行jquery报:$ is not defined
前言 背景介绍:做wap页面自动化的时候,把url地址直接输入到浏览器(chrome浏览器有手机wap模式)上测试,有个按钮死活点不到,用wap模式的触摸事件也无法解决,后来想用jquery去执行点击 ...
- 配置Ubuntu虚拟环境
1.ubuntu默认root用户没有激活,激活root用户,就要为root用户创建密码 $sudo passwd root 2.修改主机名 $vi /etc/hostname 3.安装ssh服 ...
- you boot volume has only 0 byte size
懒人方法: uname -a 列出目前使用的内核 dpkg -l | grep linux-image 列出存在的linux内核 sudo apt-get purge linux-image-3.16 ...