码迷,mamicode.com
首页 > 其他好文 > 详细

2017 新疆赛区网络赛 J Our Journey of Dalian Ends 费用流

时间:2017-09-29 21:21:25      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:ext   memset   sts   scribe   clear   ott   form   ret   ase   

题目描述:

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 Dalian ends. To be carefully considered are the following questions.

Next month in Xian, an essential lesson which we must be present had been scheduled.

But before the lesson, we need to attend a wedding in Shanghai.

We are not willing to pass through a city twice.

All available expressways between cities are known.

What we require is the shortest path, from Dalian to Xian, passing through Shanghai.

Here we go.

Input Format

There are several test cases.

The first line of input contains an integer tt which is the total number of test cases.

For each test case, the first line contains an integer m~(m\le 10000)m (m10000) which is the number of known expressways.

Each of the following mm lines describes an expressway which contains two string indicating the names of two cities and an integer indicating the length of the expressway.

The expressway connects two given cities and it is bidirectional.

Output Format

For eact test case, output the shortest path from Dalian to Xian, passing through Shanghai, or output -1?1 if it does not exist.

 

题意抽象一下即为要求从A经过B到达C的最短路,并且要求路径上的点不可重复经过。

拆点后点内限流,建立源点S指向B容量为2的一条边,A,C分别建立一条指向汇点T容量为1的边。然后依题意建图即可。

跑一遍费用流便得到答案。

  1 #include<bits/stdc++.h>
  2 #define rep(i,a,b) for(int i=a;i<=b;i++)
  3 using namespace std;
  4 const int MAXN=101000;
  5 const int maxn=20000;
  6 const int INF=~0U>>1;
  7 int maxflow=0,cost=0;
  8 int pre[maxn],vis[maxn],dis[maxn];
  9 int S,T,S1;
 10 int n,m;
 11 int tot=0;
 12 int pointer[maxn];
 13 map<string,int> mp;
 14 int shanghai;
 15 struct Edge
 16 {
 17     int to,next,cap,f,w;
 18     Edge() {};
 19     Edge(int b,int c,int nxt,int flow,int weight) {to=b,cap=c,next=nxt,f=flow,w=weight;}
 20 }edge[MAXN];
 21 inline void addedge(int a,int b,int c,int w1)
 22 {
 23     edge[tot]=Edge(b,c,pointer[a],0,w1);
 24     pointer[a]=tot++;
 25     edge[tot]=Edge(a,0,pointer[b],0,-w1);
 26     pointer[b]=tot++;
 27 }
 28 bool spfa(int s,int t)
 29 {
 30     queue<int > q;
 31     rep(i,S,T)
 32     {
 33         dis[i]=INF;
 34         vis[i]=false;
 35         pre[i]=-1;
 36     }
 37     dis[S]=0;
 38     vis[S]=1;
 39     q.push(S);
 40     while(!q.empty())
 41     {
 42         int u=q.front();q.pop();
 43         vis[u]=0;
 44         for(int j=pointer[u];j!=-1;j=edge[j].next)
 45         {
 46             int v=edge[j].to;
 47       //      if(u==92) printf("u=92 v=%d\n",v);
 48             if(edge[j].cap-edge[j].f>0&&dis[v]>dis[u]+edge[j].w)
 49             {
 50                 dis[v]=dis[u]+edge[j].w;
 51                 pre[v]=j;
 52                 if(!vis[v])
 53                 {
 54                     vis[v]=1;q.push(v);
 55                 }
 56             }
 57         }
 58     }
 59     if(pre[T]==-1) return false;
 60     else return true;
 61 }
 62 int mcmf()
 63 {
 64     int flow=0;
 65     cost=0;
 66     while(spfa(S,T))
 67     {
 68         int mi=INF;
 69         for(int i=pre[T];i!=-1;i=pre[edge[i^1].to])
 70         {
 71             mi=min(mi,edge[i].cap-edge[i].f);
 72         }
 73         for(int i=pre[T];i!=-1;i=pre[edge[i^1].to])
 74         {
 75             edge[i].f+=mi;
 76             edge[i^1].f-=mi;
 77            // printf("edge[%d].w=%d edge[i].to=%d\n",i,edge[i].w,edge[i].to);
 78             //cost+=edge[i].w*mi;
 79         }
 80        // flow+=mi;
 81     }
 82     return flow;
 83 }
 84 void Input()
 85 {
 86     mp.clear();
 87     scanf("%d",&m);
 88     string a;
 89     string b;
 90     int len;
 91     int cnt=1;
 92     memset(pointer,-1,sizeof(pointer));
 93     tot=0;
 94     rep(i,1,m)
 95     {
 96         cin>>a>>b>>len;
 97         if(mp[a]==0)
 98         {
 99             mp[a]=cnt;
100             addedge(cnt,cnt+1,1,0);
101            // if(a=="Shanghai") shanghai=cnt;
102             cnt+=2;
103 
104         }
105         if(mp[b]==0)
106         {
107             mp[b]=cnt;
108             addedge(cnt,cnt+1,1,0);
109          //   if(b=="Shanghai") shanghai=cnt;
110             cnt+=2;
111         }
112         addedge(mp[a]+1,mp[b],INF,len);
113       //  addedge(mp[b],mp[a]+1,1,len);
114         addedge(mp[b]+1,mp[a],INF,len);
115         //addedge(mp[a],mp[b]+1,1,len);
116     }
117     S=0;T=cnt;
118    // printf("shanghai=%d\n",mp["Shanghai"]);
119     addedge(mp["Shanghai"],mp["Shanghai"]+1,1,0);
120     addedge(S,mp["Shanghai"],2,0);
121     addedge(mp["Xian"]+1,T,1,0);
122     addedge(mp["Dalian"]+1,T,1,0);
123 }
124 int main()
125 {
126    // freopen("in.txt","r",stdin);
127     int T;
128     scanf("%d",&T);
129     rep(t1,1,T)
130     {
131         Input();
132         maxflow=mcmf();
133         if(maxflow==2) printf("%d\n",cost);
134         else printf("-1\n");
135     }
136     return 0;
137 }

 

2017 新疆赛区网络赛 J Our Journey of Dalian Ends 费用流

标签:ext   memset   sts   scribe   clear   ott   form   ret   ase   

原文地址:http://www.cnblogs.com/zhixingr/p/7612369.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!