乌鲁木齐网络赛J题(最小费用最大流模板)
生活随笔
收集整理的這篇文章主要介紹了
乌鲁木齐网络赛J题(最小费用最大流模板)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
ACM ICPC 烏魯木齊網絡賽 J. Our Journey of Dalian Ends
2017-09-09 17:24?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,否則輸出最小花費。
- #include<stdio.h>??
- #include<algorithm>??
- #include<string.h>??
- #include<map>??
- #include<queue>??
- #include<string>??
- using?namespace?std;??
- #define?ll?long?long??
- const?ll?maxm?=?10005;??
- const?ll?INF?=?1e18?+?7;??
- struct?node??
- {??
- ????ll?u,?v,?flow,?cost,?next;??
- }edge[maxm?*?10];??
- map<string,?ll>p;??
- ll?cnt,?s,?t,?n,?m,?sum,?FLOW;??
- ll?head[maxm?*?10],?dis[maxm?*?10],?pre[maxm?*?10];??
- char?a[maxm],?b[maxm];??
- void?init()??
- {??
- ????p.clear();??
- ????cnt?=?0,?s?=?0,?t?=?n?*?5?+?1,?sum?=?0,?FLOW?=?0;??
- ????memset(head,?-1,?sizeof(head));??
- }??
- void?add(ll?u,?ll?v,?ll?flow,?ll?cost)??
- {??
- ????edge[cnt].u?=?u,?edge[cnt].v?=?v;??
- ????edge[cnt].flow?=?flow,?edge[cnt].cost?=?cost;??
- ????edge[cnt].next?=?head[u],?head[u]?=?cnt++;??
- ????edge[cnt].u?=?v,?edge[cnt].v?=?u;??
- ????edge[cnt].flow?=?0,?edge[cnt].cost?=?-cost;??
- ????edge[cnt].next?=?head[v],?head[v]?=?cnt++;??
- }??
- ll?bfs()??
- {??
- ????queue<ll>q;??
- ????for?(ll?i?=?0;i?<=?t;i++)?dis[i]?=?INF;??
- ????memset(pre,?-1,?sizeof(pre));??
- ????dis[s]?=?0,?q.push(s);??
- ????ll?rev?=?0;??
- ????while?(!q.empty())??
- ????{??
- ????????ll?u?=?q.front();q.pop();??
- ????????for?(ll?i?=?head[u];i?!=?-1;i?=?edge[i].next)??
- ????????{??
- ????????????ll?v?=?edge[i].v;??
- ????????????if?(dis[v]?>?dis[u]?+?edge[i].cost&&edge[i].flow)??
- ????????????{??
- ????????????????dis[v]?=?dis[u]?+?edge[i].cost;??
- ????????????????pre[v]?=?i,?q.push(v);??
- ????????????}??
- ????????}??
- ????}??
- ????if?(dis[t]?==?INF)?return?0;??
- ????return?1;??
- }??
- ll?MCMF()??
- {??
- ????ll?ans?=?0,?minflow;??
- ????while?(bfs())??
- ????{??
- ????????minflow?=?INF;??
- ????????for?(ll?i?=?pre[t];i?!=?-1;i?=?pre[edge[i].u])??
- ????????????minflow?=?min(minflow,?edge[i].flow);??
- ????????for?(ll?i?=?pre[t];i?!=?-1;i?=?pre[edge[i].u])??
- ????????????edge[i].flow?-=?minflow,?edge[i?^?1].flow?+=?minflow;??
- ????????ans?+=?dis[t]?*?minflow;??
- ????????FLOW?+=?minflow;??
- ????}??
- ????return?ans;??
- }??
- int?main()??
- {??
- ????ll?i,?j,?k,?T,?c;??
- ????scanf("%lld",?&T);??
- ????while?(T--)??
- ????{??
- ????????scanf("%lld",?&n);??
- ????????init();??
- ????????ll?nn?=?n?*?2;??
- ????????for?(i?=?1;i?<=?n;i++)??
- ????????{??
- ????????????scanf("%s%s%lld",?a,?b,?&c);??
- ????????????if?(p[a]?==?0)??
- ????????????{??
- ????????????????p[a]?=?++sum,?k?=?1;??
- ????????????????if?(strcmp(a,?"Shanghai")?==?0)?k?=?2;??
- ????????????????add(p[a],?p[a]?+?nn,?k,?0);??
- ????????????}??
- ????????????if?(p[b]?==?0)??
- ????????????{??
- ????????????????p[b]?=?++sum,?k?=?1;??
- ????????????????if?(strcmp(b,?"Shanghai")?==?0)?k?=?2;??
- ????????????????add(p[b],?p[b]?+?nn,?k,?0);??
- ????????????}??
- ????????????ll?u?=?p[a],?v?=?p[b];??
- ????????????add(u?+?nn,?v,?INF,?c);??
- ????????????add(v?+?nn,?u,?INF,?c);??
- ????????}??
- ????????ll?u?=?p["Dalian"];??
- ????????add(s,?u,?1,?0);??
- ????????u?=?p["Xian"];??
- ????????add(s,?u,?1,?0);??
- ????????u?=?p["Shanghai"];??
- ????????add(u?+?nn,?t,?2,?0);??
- ????????ll?ans?=?MCMF();??
- ????????if?(FLOW?==?2)?printf("%lld\n",?ans);??
- ????????else?printf("-1\n");??
- ????}??
- ????return?0;??
- }??
總結
以上是生活随笔為你收集整理的乌鲁木齐网络赛J题(最小费用最大流模板)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 姐妹们,两个月没来月经了,求助方法啊?[
- 下一篇: 求一个好听的冰淇淋名字