BZOJ 1070: [SCOI2007]修车(费用流)

http://www.lydsy.com/JudgeOnline/problem.php?id=1070

题意:

 

思路:

神奇的构图。

因为排在后面的人需要等待前面的车修好,这里将每个技术人员拆成n个点,第k个点表示该技术人员倒数第k的顺序来修理该车,此时它的时间对于答案的贡献就是kw。

最后跑一遍最小费用流。

  1 #include<iostream>
  2 #include<algorithm>
  3 #include<cstring>
  4 #include<cstdio>
  5 #include<vector>
  6 #include<stack>
  7 #include<queue>
  8 #include<cmath>
  9 #include<map>
 10 #include<set>
 11 using namespace std;
 12 typedef long long ll;
 13 typedef pair<int,int> pll;
 14 const int INF = 0x3f3f3f3f;
 15 const int maxn = 1000 + 5;
 16 
 17 int n,m;
 18 
 19 struct Edge
 20 {
 21     int from, to, cap, flow, cost;
 22     Edge(int u, int v, int c, int f, int w) :from(u), to(v), cap(c), flow(f), cost(w) {}
 23 };
 24 
 25 struct MCMF
 26 {
 27     int n, m;
 28     vector<Edge> edges;
 29     vector<int> G[maxn];
 30     int inq[maxn];
 31     int d[maxn];
 32     int p[maxn];
 33     int a[maxn];
 34 
 35     void init(int n)
 36     {
 37         this->n = n;
 38         for (int i = 0; i<n; i++) G[i].clear();
 39         edges.clear();
 40     }
 41 
 42     void AddEdge(int from, int to, int cap, int cost)
 43     {
 44         edges.push_back(Edge(from, to, cap, 0, cost));
 45         edges.push_back(Edge(to, from, 0, 0, -cost));
 46         m = edges.size();
 47         G[from].push_back(m - 2);
 48         G[to].push_back(m - 1);
 49     }
 50 
 51     bool BellmanFord(int s, int t, int &flow, int & cost)
 52     {
 53         for (int i = 0; i<n; i++) d[i] = INF;
 54         memset(inq, 0, sizeof(inq));
 55         d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = INF;
 56 
 57         queue<int> Q;
 58         Q.push(s);
 59         while (!Q.empty()){
 60             int u = Q.front(); Q.pop();
 61             inq[u] = 0;
 62             for (int i = 0; i<G[u].size(); i++){
 63                 Edge& e = edges[G[u][i]];
 64                 if (e.cap>e.flow && d[e.to]>d[u] + e.cost){
 65                     d[e.to] = d[u] + e.cost;
 66                     p[e.to] = G[u][i];
 67                     a[e.to] = min(a[u], e.cap - e.flow);
 68                     if (!inq[e.to]) { Q.push(e.to); inq[e.to] = 1; }
 69                 }
 70             }
 71         }
 72 
 73         if (d[t] == INF) return false;
 74         flow += a[t];
 75         cost += d[t] * a[t];
 76         for (int u = t; u != s; u = edges[p[u]].from)
 77         {
 78             edges[p[u]].flow += a[t];
 79             edges[p[u] ^ 1].flow -= a[t];
 80         }
 81         return true;
 82     }
 83 
 84     int MincostMaxdflow(int s, int t){
 85         int flow = 0, cost = 0;
 86         while (BellmanFord(s, t, flow, cost));
 87         return cost;
 88     }
 89 }t;
 90 
 91 
 92 int main()
 93 {
 94     //freopen("in.txt","r",stdin);
 95     while(~scanf("%d%d",&m,&n))
 96     {
 97         int src=0,dst=n*m+n+1;
 98         t.init(dst+1);
 99         for(int i=1;i<=n;i++)
100         {
101             t.AddEdge(src,i,1,0);
102             for(int j=1;j<=m;j++)
103             {
104                 int x; scanf("%d",&x);
105                 for(int k=1;k<=n;k++)
106                     t.AddEdge(i,j*n+k,1,k*x);
107             }
108         }
109         for(int j=n+1;j<=m*n+n;j++)
110             t.AddEdge(j,dst,1,0);
111         int ans = t.MincostMaxdflow(src,dst);
112         printf("%.2f\n",(double)ans/n);
113     }
114     return 0;
115 }

 

posted @ 2017-10-12 21:38  Kayden_Cheung  阅读(170)  评论(0编辑  收藏  举报
//目录