乌鲁木齐网络赛J题(最小费用最大流模板)

ACM ICPC 乌鲁木齐网络赛 J. Our Journey of Dalian Ends

  243人阅读  评论(0)  收藏  举报
  分类:
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 (m≤10000) 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.


样例输入


3
2
Dalian Shanghai 3
Shanghai Xian 4
5
Dalian Shanghai 7
Shanghai Nanjing 1
Dalian Nanjing 3
Nanjing Xian 5
Shanghai Xian 8
3
Dalian Nanjing 6
Shanghai Nanjing 7
Nanjing Xian 8
样例输出


7
12

-1


每个城市拆成出点和入点,源点连西安和大连,汇点连上海,相当于求从西安到上海和从大连到上海最小距离之和,每个城市入点和出点之间连一条容量为1的边,但是注意,上海的容量必须是2,再根据给出的边,分别连接出点入点,存入相应花费,那么问题就可以转化成最小费用最大流了,如果流量不为2输出-1,否则输出最小花费。


[cpp]  view plain  copy
  1. #include<stdio.h>  
  2. #include<algorithm>  
  3. #include<string.h>  
  4. #include<map>  
  5. #include<queue>  
  6. #include<string>  
  7. using namespace std;  
  8. #define ll long long  
  9. const ll maxm = 10005;  
  10. const ll INF = 1e18 + 7;  
  11. struct node  
  12. {  
  13.     ll u, v, flow, cost, next;  
  14. }edge[maxm * 10];  
  15. map<string, ll>p;  
  16. ll cnt, s, t, n, m, sum, FLOW;  
  17. ll head[maxm * 10], dis[maxm * 10], pre[maxm * 10];  
  18. char a[maxm], b[maxm];  
  19. void init()  
  20. {  
  21.     p.clear();  
  22.     cnt = 0, s = 0, t = n * 5 + 1, sum = 0, FLOW = 0;  
  23.     memset(head, -1, sizeof(head));  
  24. }  
  25. void add(ll u, ll v, ll flow, ll cost)  
  26. {  
  27.     edge[cnt].u = u, edge[cnt].v = v;  
  28.     edge[cnt].flow = flow, edge[cnt].cost = cost;  
  29.     edge[cnt].next = head[u], head[u] = cnt++;  
  30.     edge[cnt].u = v, edge[cnt].v = u;  
  31.     edge[cnt].flow = 0, edge[cnt].cost = -cost;  
  32.     edge[cnt].next = head[v], head[v] = cnt++;  
  33. }  
  34. ll bfs()  
  35. {  
  36.     queue<ll>q;  
  37.     for (ll i = 0;i <= t;i++) dis[i] = INF;  
  38.     memset(pre, -1, sizeof(pre));  
  39.     dis[s] = 0, q.push(s);  
  40.     ll rev = 0;  
  41.     while (!q.empty())  
  42.     {  
  43.         ll u = q.front();q.pop();  
  44.         for (ll i = head[u];i != -1;i = edge[i].next)  
  45.         {  
  46.             ll v = edge[i].v;  
  47.             if (dis[v] > dis[u] + edge[i].cost&&edge[i].flow)  
  48.             {  
  49.                 dis[v] = dis[u] + edge[i].cost;  
  50.                 pre[v] = i, q.push(v);  
  51.             }  
  52.         }  
  53.     }  
  54.     if (dis[t] == INF) return 0;  
  55.     return 1;  
  56. }  
  57. ll MCMF()  
  58. {  
  59.     ll ans = 0, minflow;  
  60.     while (bfs())  
  61.     {  
  62.         minflow = INF;  
  63.         for (ll i = pre[t];i != -1;i = pre[edge[i].u])  
  64.             minflow = min(minflow, edge[i].flow);  
  65.         for (ll i = pre[t];i != -1;i = pre[edge[i].u])  
  66.             edge[i].flow -= minflow, edge[i ^ 1].flow += minflow;  
  67.         ans += dis[t] * minflow;  
  68.         FLOW += minflow;  
  69.     }  
  70.     return ans;  
  71. }  
  72. int main()  
  73. {  
  74.     ll i, j, k, T, c;  
  75.     scanf("%lld", &T);  
  76.     while (T--)  
  77.     {  
  78.         scanf("%lld", &n);  
  79.         init();  
  80.         ll nn = n * 2;  
  81.         for (i = 1;i <= n;i++)  
  82.         {  
  83.             scanf("%s%s%lld", a, b, &c);  
  84.             if (p[a] == 0)  
  85.             {  
  86.                 p[a] = ++sum, k = 1;  
  87.                 if (strcmp(a, "Shanghai") == 0) k = 2;  
  88.                 add(p[a], p[a] + nn, k, 0);  
  89.             }  
  90.             if (p[b] == 0)  
  91.             {  
  92.                 p[b] = ++sum, k = 1;  
  93.                 if (strcmp(b, "Shanghai") == 0) k = 2;  
  94.                 add(p[b], p[b] + nn, k, 0);  
  95.             }  
  96.             ll u = p[a], v = p[b];  
  97.             add(u + nn, v, INF, c);  
  98.             add(v + nn, u, INF, c);  
  99.         }  
  100.         ll u = p["Dalian"];  
  101.         add(s, u, 1, 0);  
  102.         u = p["Xian"];  
  103.         add(s, u, 1, 0);  
  104.         u = p["Shanghai"];  
  105.         add(u + nn, t, 2, 0);  
  106.         ll ans = MCMF();  
  107.         if (FLOW == 2) printf("%lld\n", ans);  
  108.         else printf("-1\n");  
  109.     }  
  110.     return 0;  
  111. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值