【HDOJ】3315 My Brute

几乎与2853相同,Successive Shortest Path Algorithm(SSPA) 解费用流。

  1 /* 3315 */
  2 #include <iostream>
  3 #include <string>
  4 #include <map>
  5 #include <queue>
  6 #include <set>
  7 #include <stack>
  8 #include <vector>
  9 #include <deque>
 10 #include <algorithm>
 11 #include <cstdio>
 12 #include <cmath>
 13 #include <ctime>
 14 #include <cstring>
 15 #include <climits>
 16 #include <cctype>
 17 #include <cassert>
 18 #include <functional>
 19 #include <iterator>
 20 #include <iomanip>
 21 using namespace std;
 22 //#pragma comment(linker,"/STACK:102400000,1024000")
 23 
 24 #define sti                set<int>
 25 #define stpii            set<pair<int, int> >
 26 #define mpii            map<int,int>
 27 #define vi                vector<int>
 28 #define pii                pair<int,int>
 29 #define vpii            vector<pair<int,int> >
 30 #define rep(i, a, n)     for (int i=a;i<n;++i)
 31 #define per(i, a, n)     for (int i=n-1;i>=a;--i)
 32 #define clr                clear
 33 #define pb                 push_back
 34 #define mp                 make_pair
 35 #define fir                first
 36 #define sec                second
 37 #define all(x)             (x).begin(),(x).end()
 38 #define SZ(x)             ((int)(x).size())
 39 #define lson            l, mid, rt<<1
 40 #define rson            mid+1, r, rt<<1|1
 41 
 42 const int INF = 0x1f1f1f1f;
 43 const int maxn = 105;
 44 const int maxv = maxn*2;
 45 const int maxe = maxv * maxv * 5;
 46 int V[maxe], F[maxe], W[maxe], nxt[maxe];
 47 int head[maxv], pre[maxv], ID[maxv], dis[maxv], cnt[maxv];
 48 bool visit[maxv];
 49 int Hi[maxn], Pi[maxn], Ai[maxn], Bi[maxn], Vi[maxn];
 50 int s, t, m;
 51 
 52 bool win(int i, int j) {
 53     int c1 = (Pi[j] + Ai[i] - 1) / Ai[i];
 54     int c2 = (Hi[i] + Bi[j] - 1) / Bi[j];
 55 
 56     return c1 <= c2;
 57 }
 58 
 59 void addEdge(int u, int v, int c, int w) {
 60     V[m] = v;
 61     F[m] = c;
 62     W[m] = w;
 63     nxt[m] = head[u];
 64     head[u] = m++;
 65 
 66     V[m] = u;
 67     F[m] = 0;
 68     W[m] = -w;
 69     nxt[m] = head[v];
 70     head[v] = m++;
 71 }
 72 
 73 void Bellman_Ford(int st, int ed) {
 74     int u, v, k;
 75     queue<int> Q;
 76 
 77     memset(dis, INF, sizeof(dis));
 78     memset(cnt, 0, sizeof(cnt));
 79     memset(pre, -1, sizeof(pre));
 80     dis[st] = 0;
 81     cnt[st] = 1;
 82     Q.push(st);
 83 
 84     while (!Q.empty()) {
 85         u = Q.front();
 86         Q.pop();
 87         cnt[u] = -cnt[u];
 88         for (k=head[u]; k!=-1; k=nxt[k]) {
 89             v = V[k];
 90             if (F[k] && dis[v]>dis[u]+W[k]) {
 91                 dis[v] = dis[u] + W[k];
 92                 ID[v] = k;
 93                 pre[v] = u;
 94                 if (cnt[v] <= 0) {
 95                     cnt[v] = -cnt[v] + 1;
 96                     Q.push(v);
 97                 }
 98             }
 99         }
100     }
101 }
102 
103 bool bfs(int st, int ed) {
104     Bellman_Ford(st, ed);
105     priority_queue<pii, vector<pii>, greater<pii> > Q;
106     int u, v, k;
107 
108     Q.push(make_pair(0, st));
109     while (!Q.empty()) {
110         pii p = Q.top();
111         Q.pop();
112         u = p.second;
113         if (dis[p.second] > p.first) continue;
114         if (u == ed) break;
115         for (k=head[u]; k!=-1; k=nxt[k]) {
116             v = V[k];
117             if (F[k] && dis[v]>dis[u]+W[k]) {
118                 dis[v] = dis[u] + W[k];
119                 ID[v] = k;
120                 pre[v] = u;
121                 Q.push(make_pair(dis[v], v));
122             }
123         }
124     }
125 
126     return pre[ed] == -1;
127 }
128 
129 int MCMF(int s, int t) {
130     int ret = 0, tmp;
131     int u, v, k;
132 
133     while (1) {
134         if (bfs(s, t))
135             break;
136 
137         tmp = INF;
138         for (v=t, u=pre[v]; v!=s; v=u, u=pre[v]) {
139             k = ID[v];
140             tmp = min(F[k], tmp);
141         }
142 
143         for (v=t, u=pre[v]; v!=s; v=u, u=pre[v]) {
144             k = ID[v];
145             F[k] -= tmp;
146             F[k^1] += tmp;
147         }
148 
149         ret += dis[t] * tmp;
150     }
151 
152     return ret;
153 }
154 
155 int main() {
156     ios::sync_with_stdio(false);
157     #ifndef ONLINE_JUDGE
158         freopen("data.in", "r", stdin);
159         freopen("data.out", "w", stdout);
160     #endif
161 
162     int n;
163     int ans, tmp;
164 
165     while (scanf("%d", &n)!=EOF && n) {
166         rep(i, 1, n+1)
167             scanf("%d", &Vi[i]);
168         rep(i, 1, n+1)
169             scanf("%d", &Hi[i]);
170         rep(i, 1, n+1)
171             scanf("%d", &Pi[i]);
172         rep(i, 1, n+1)
173             scanf("%d", &Ai[i]);
174         rep(i, 1, n+1)
175             scanf("%d", &Bi[i]);
176 
177         s = m = 0;
178         t = n + n + 1;
179         memset(head, -1, sizeof(head));
180 
181         rep(i, 1, n+1) {
182             rep(j, 1, n+1) {
183                 if (win(i, j)) {
184                     if (i == j) {
185                         tmp = Vi[i] * 100 + 1;
186                     } else {
187                         tmp = Vi[i] * 100;
188                     }
189                 } else {
190                     if (i == j) {
191                         tmp = -Vi[i] * 100 + 1;
192                     } else {
193                         tmp = -Vi[i] * 100;
194                     }
195                 }
196                 addEdge(i, n+j, 1, -tmp);
197             }
198         }
199 
200         rep(i, 1, n+1) {
201             addEdge(s, i, 1, 0);
202             addEdge(n+i, t, 1, 0);
203         }
204 
205         ans = -MCMF(s, t);
206         if (ans < 100) {
207             puts("Oh, I lose my dear seaco!");
208         } else {
209             printf("%d %.03f%%\n", ans/100, ans%100*100.0/n);
210         }
211     }
212 
213     #ifndef ONLINE_JUDGE
214         printf("time = %d.\n", (int)clock());
215     #endif
216 
217     return 0;
218 }

 

posted on 2015-10-17 16:20  Bombe  阅读(205)  评论(0编辑  收藏  举报

导航