POJ 3228 Gold Transportation

Gold Transportation

Time Limit: 2000ms

Memory Limit: 65536KB

This problem will be judged on PKU. Original ID: 3228
64-bit integer IO format: %lld      Java class name: Main

Recently, a number of gold mines have been discovered in Zorroming State. To protect this treasure, we must transport this gold to the storehouses as quickly as possible. Suppose that the Zorroming State consists of N towns and there are M bidirectional roads among these towns. The gold mines are only discovered in parts of the towns, while the storehouses are also owned by parts of the towns. The storage of the gold mine and storehouse for each town is finite. The truck drivers in the Zorroming State are famous for their bad temper that they would not like to drive all the time and they need a bar and an inn available in the trip for a good rest. Therefore, your task is to minimize the maximum adjacent distance among all the possible transport routes on the condition that all the gold is safely transported to the storehouses.

Input

The input contains several test cases. For each case, the first line is integer N(1<=N<=200). The second line is N integers associated with the storage of the gold mine in every towns .The third line is also N integers associated with the storage of the storehouses in every towns .Next is integer M(0<=M<=(n-1)*n/2).Then M lines follow. Each line is three integers x y and d(1<=x,y<=N,0<d<=10000), means that there is a road between x and y for distance of d. N=0 means end of the input.

Output

For each case, output the minimum of the maximum adjacent distance on the condition that all the gold has been transported to the storehouses or "No Solution".

Sample Input

4
3 2 0 0
0 0 3 3
6
1 2 4
1 3 10
1 4 12
2 3 6
2 4 8
3 4 5
0

Sample Output

6

Source

South Central China 2007 hosted by NUDT

解题:二分距离。求最小的最大距离。。。

源点与宝矿连接,容量为该矿的容量,汇点与藏点连接,容量为藏地的容量。矿 和 藏地的距离进行枚举

此题为什么如何建图,我还是有点不明白,奇葩的建图过程。

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <climits>
  7 #include <vector>
  8 #include <queue>
  9 #include <cstdlib>
 10 #include <string>
 11 #include <set>
 12 #include <stack>
 13 #define LL long long
 14 #define pii pair<int,int>
 15 #define INF 0x3f3f3f3f
 16 using namespace std;
 17 const int maxn = 210;
 18 struct arc {
 19     int to,flow,next;
 20     arc(int x = 0,int y = 0,int z = -1) {
 21         to = x;
 22         flow = y;
 23         next = z;
 24     }
 25 };
 26 arc e[maxn*maxn];
 27 int head[maxn],d[maxn],gold[maxn],store[maxn];
 28 int tot,n,m,S,T,cur[maxn],q[maxn],hd,tl;
 29 int a[maxn*maxn],b[maxn*maxn],c[maxn*maxn];
 30 void add(int u,int v,int w) {
 31     e[tot] = arc(v,w,head[u]);
 32     head[u] = tot++;
 33     e[tot] = arc(u,0,head[v]);
 34     head[v] = tot++;
 35 }
 36 void build(int mid) {
 37     memset(head,-1,sizeof(head));
 38     tot = 0;
 39     for(int i = 0; i < m; i++)
 40         if(c[i] <= mid) {
 41             add(a[i],b[i],INF);
 42             add(b[i],a[i],INF);
 43         }
 44     for(int i = 1; i <= n; i++)
 45         add(S,i,gold[i]);
 46     for(int i = 1; i <= n; i++)
 47         add(i,T,store[i]);
 48 }
 49 bool bfs() {
 50     memset(d,-1,sizeof(d));
 51     hd = tl = 0;
 52     q[tl++] = S;
 53     d[S] = 1;
 54     while(hd < tl) {
 55         int u = q[hd++];
 56         for(int i = head[u]; ~i; i = e[i].next) {
 57             if(d[e[i].to] == -1 && e[i].flow > 0) {
 58                 d[e[i].to] = d[u] + 1;
 59                 q[tl++] = e[i].to;
 60             }
 61         }
 62     }
 63     return d[T] > -1;
 64 }
 65 int dfs(int u,int low) {
 66     if(u == T) return low;
 67     int tmp = 0,a;
 68     for(int &i = cur[u]; ~i; i = e[i].next) {
 69         if(e[i].flow > 0 && d[e[i].to] == d[u] + 1 && (a = dfs(e[i].to,min(low,e[i].flow)))) {
 70             tmp += a;
 71             low -= a;
 72             e[i].flow -= a;
 73             e[i^1].flow += a;
 74             if(!low) break;
 75         }
 76     }
 77     if(!tmp) d[u] = -1;
 78     return tmp;
 79 }
 80 int dinic() {
 81     int tmp = 0;
 82     while(bfs()) {
 83         memcpy(cur,head,sizeof(head));
 84         tmp += dfs(S,INF);
 85     }
 86     return tmp;
 87 }
 88 int main() {
 89     int suma,sumb,high,low,ans;
 90     while(scanf("%d",&n),n) {
 91         suma = sumb = 0;
 92         for(int i = 1; i <= n; i++) {
 93             scanf("%d",gold+i);
 94             suma += gold[i];
 95         }
 96         for(int i = 1; i <= n; i++) {
 97             scanf("%d",store+i);
 98             sumb += store[i];
 99         }
100         scanf("%d",&m);
101         low = INF;
102         high = -1;
103         for(int i = 0; i < m; i++) {
104             scanf("%d %d %d",a+i,b+i,c+i);
105             low = min(low,c[i]);
106             high = max(high,c[i]);
107         }
108         if(suma > sumb) {
109             puts("No Solution");
110             continue;
111         }
112         ans = -1;
113         S = 0;
114         T = n + 1;
115         while(low <= high) {
116             int mid = (low + high)>>1;
117             build(mid);
118             if(dinic() >= suma) {
119                 ans = mid;
120                 high = mid - 1;
121             } else low = mid + 1;
122         }
123         if(ans > 0) printf("%d\n",ans);
124         else puts("No Solution");
125     }
126     return 0;
127 }

时间: 2024-08-01 06:29:25

POJ 3228 Gold Transportation的相关文章

poj 3228 Gold Transportation 并查集

题意: 有n和城市和m条路,每个城市都有产生金量和收集金量,现在要把所有黄金收集,求经过的最短边是多少. 分析: 二分+最大流或用并查集合并等价类. //poj 3228 //sep9 #include <iostream> #include <algorithm> using namespace std; const int maxN=256; const int maxM=10024; int p[maxN],sum[maxN]; struct Edge { int u,v,w

POJ 3228 -- Gold Transportation【二分 &amp;&amp; 最大流】

Gold Transportation Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3079   Accepted: 1101 Description Recently, a number of gold mines have been discovered in Zorroming State. To protect this treasure, we must transport this gold to the

poj 3228 Gold Transportation 二分+最大流

http://poj.org/problem?id=3228 题意:有n个城市,每个城市有一定量的金子,我们需要把所有的金子都存到城市中的仓库中,有一些道路连通这些城市,每条道路都有自己的花费,要求的是,把所有的金子都运到仓库中,所走的那些道路,其中花费的最大值最小是多少. 思路,二分答案,把花费比答案小的那些道路建成一个图,再建立一个源点和所有城市相连,流量是每个城市的金子数量,再建立一个汇点,连接所有的城市,流量是每个城市金子的最大存储量.再跑一遍最大流,如果流量等于所有的金子和,那么就可以

POJ 1797 Heavy Transportation (最短路变形)

Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 20364   Accepted: 5401 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man

[ACM] POJ 2000 Gold Coins

Gold Coins Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20913   Accepted: 13098 Description The king pays his loyal knight in gold coins. On the first day of his service, the knight receives one gold coin. On each of the next two days

poj 1797 Heavy Transportation(最大生成树)

poj 1797 Heavy Transportation Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has

poj 3274 -- Gold Balanced Lineup

Gold Balanced Lineup Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12110   Accepted: 3553 Description Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been able to narrow down the list of features shared

(贪心+并查集) poj 3228

Gold Transportation Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3060   Accepted: 1089 Description Recently, a number of gold mines have been discovered in Zorroming State. To protect this treasure, we must transport this gold to the

poj 3274 Gold Balanced Lineup, 拉链式hash

sum[i][j] 表示从第1到第i头cow属性j的出现次数 所以题目要求等价为: 求满足 sum[i][0]-sum[j][0]=sum[i][1]-sum[j][1]=.....=sum[i][k-1]-sum[j][k-1] (j<i) 中最大的i-j 将上式变换可得到 sum[i][1]-sum[i][0] = sum[j][1]-sum[j][0] sum[i][2]-sum[i][0] = sum[j][2]-sum[j][0] ...... sum[i][k-1]-sum[i][0