zoukankan      html  css  js  c++  java
  • 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;

      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             if(!mp[str1]) mp[str1]=co++;
    118             if(!mp[str2]) mp[str2]=co++;
    119             add(mp[str1]+n+n,mp[str2],INF,w);
    120             add(mp[str2]+n+n,mp[str1],INF,w);
    121         }
    122         s=0,t=4*n+1;
    123         map<string,int>::iterator it;
    124         for(it=mp.begin();it!=mp.end();it++){
    125            if(it->first=="Xian"){
    126                 add(s,it->second,1,0);
    127                 add(it->second,it->second+2*n,1,0);
    128             }
    129             else if(it->first=="Qingdao"){
    130                 add(s,it->second,2,0);
    131                 add(it->second,it->second+2*n,2,0);
    132             }
    133             else if(it->first=="Hongqiao"){
    134                 add(it->second+2*n,t,2,0);
    135                 add(it->second,it->second+2*n,2,0);
    136             }
    137             else if(it->first=="Pudong"){
    138                 add(it->second+2*n,t,1,0);
    139                 add(it->second,it->second+2*n,1,0);
    140             }
    141             else{
    142                 add(it->second,it->second+2*n,1,0);
    143             }
    144         }
    145         int ans=MCMF(s,t,4*n+2);
    146         if(maxflow==3) cout<<ans<<endl;
    147         else cout<<-1<<endl;
    148     }
    149 }
    View Code
  • 相关阅读:
    HBase 负载均衡
    HBase的写事务,MVCC及新的写线程模型
    HBase RegionServer宕机处理恢复
    分布式事务实现-Percolator
    MVC框架
    06-JS中li移动第二种形式
    05-JS中li移动第一种形式
    04-JS中文档碎片
    03-JS中添加节点
    02-JS中父节点
  • 原文地址:https://www.cnblogs.com/Fighting-sh/p/9892225.html
Copyright © 2011-2022 走看看