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

Our Journey of Xian Ends

时间:2018-02-28 12:55:28      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:char   ==   ems   src   close   sed   dao   csdn   get   

Our Journey of Xian Ends

链接:here

参考http://blog.csdn.net/wangshuhe963/article/details/78516821

费用流~

技术分享图片
  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 #define LL long long
  4 const int maxv = 40010;
  5 const int maxe = 40010;
  6 const int inf = 0x3f3f3f3f;
  7 struct Edge{
  8     int head[maxv], nxt[maxe<<1], from[maxe<<1], to[maxe<<1], cap[maxe<<1], flow[maxe<<1], cost[maxe<<1];
  9     int cnt;
 10     void init(){
 11         cnt = 0;
 12         memset(head, -1, sizeof(head));
 13     }
 14     void add(int u, int v, int cp, int cst){
 15         from[cnt] = u;
 16         to[cnt] = v;
 17         nxt[cnt] = head[u];
 18         cap[cnt] = cp; flow[cnt] = 0; cost[cnt] = cst;
 19         head[u] = cnt++;
 20     }
 21 };
 22 struct MCMF{
 23     int n, m, s, t;
 24     Edge e;
 25     int inq[maxv], d[maxv], p[maxv], a[maxv];
 26 
 27     void init(int _n){
 28         n = _n;
 29         e.init();
 30     }
 31     void adde(int u, int v, int cap, int cost){
 32         e.add(u, v, cap, cost);
 33         e.add(v, u, 0, -cost);
 34     }
 35     bool bellmanFord(int s, int t, int &flow, int &cost){
 36         for(int i = 0; i < n; i++) d[i] = inf;
 37         memset(inq, 0, sizeof(inq));
 38         d[s] = 0; inq[s] = 1; p[s] = -1; a[s] = inf;
 39         queue<int> q;
 40         q.push(s);
 41         while(!q.empty()){
 42             int u = q.front(); q.pop();
 43             inq[u] = 0;
 44             for(int i = e.head[u]; ~i; i = e.nxt[i]){
 45                 int v = e.to[i];
 46                 if(e.cap[i] > e.flow[i] && d[v] > d[u] + e.cost[i]){
 47                     d[v] = d[u] + e.cost[i];
 48                     p[v] = i;
 49                     a[v] = min(a[u], e.cap[i] - e.flow[i]);
 50                     if(!inq[v]) q.push(v), inq[v] = 1;
 51                 }
 52             }
 53         }
 54         if(d[t] == inf) return 0;
 55         flow += a[t]; 
 56         cost += a[t] * d[t];
 57         int u = t;
 58         while(u != s){
 59             e.flow[p[u]] += a[t];
 60             e.flow[p[u] ^ 1] -= a[t];
 61             u = e.from[p[u]];
 62         }
 63         return 1;
 64     }
 65     int mincost(int s, int t){
 66         int flow = 0, cost = 0;
 67         while(bellmanFord(s, t, flow, cost));
 68         if(flow == 3) return cost;
 69         else return -1;
 70     }
 71 }solve;
 72 int cnt;
 73 map<string, int> mp;
 74 int ID(char *s){
 75     if(!mp.count(s)) mp[s] = cnt++;
 76     return mp[s];
 77 }
 78 int main(){
 79     int t;
 80     scanf("%d", &t);
 81     while(t--){
 82         mp.clear();
 83         cnt = 0;
 84         int n, m;
 85         scanf("%d", &m);
 86         n = m * 2;
 87         solve.init(n * 2 + 2);
 88         for(int i = 0; i < m; i++){
 89             char u[12], v[12];
 90             int w;
 91             scanf("%s %s %d", u, v, &w);
 92             int uu = ID(u), vv = ID(v);
 93             solve.adde(uu + n, vv, inf, w);
 94             solve.adde(vv + n, uu, inf, w);
 95         }
 96         int S = n * 2, T = n * 2 + 1;
 97         map<string, int> ::iterator it;
 98         for(it = mp.begin(); it != mp.end(); it++){
 99             int u = it->second;
100             if(it->first == "Xian"){
101                 solve.adde(u, u + n, 1, 0);
102                 solve.adde(u + n, T, 1, 0);
103             }else if(it->first == "Qingdao"){
104                 solve.adde(u, u + n, 2, 0);
105                 solve.adde(u + n, T, 2, 0);
106             }else if(it->first == "Hongqiao"){
107                 solve.adde(u, u + n, 2, 0);
108                 solve.adde(S, u, 2, 0);
109             }else if(it->first == "Pudong"){
110                 solve.adde(u, u + n, 1, 0);
111                 solve.adde(S, u, 1, 0);
112             }else{
113                 solve.adde(u, u + n, 1, 0);
114             }
115         }
116         solve.s = S; solve.t = T;
117         int ans = solve.mincost(S, T);
118         cout<<ans<<endl;
119     }
120 }
View Code

 

Our Journey of Xian Ends

标签:char   ==   ems   src   close   sed   dao   csdn   get   

原文地址:https://www.cnblogs.com/yijiull/p/8482908.html

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