日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

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

發布時間:2023/11/27 生活经验 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 乌鲁木齐网络赛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 plaincopy
  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. }??

總結

以上是生活随笔為你收集整理的乌鲁木齐网络赛J题(最小费用最大流模板)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。