http://poj.org/problem?id=2112_网络流

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 using namespace std;
 7
 8 #define MAX 300
 9 #define INF 10000000
10 int dis[MAX][MAX];  // 任意两点间的最短路径
11 int map[MAX][MAX];  // 容量网络
12 bool sign[MAX][MAX];  // 层次网络
13 bool used[MAX];  // 标志数组
14 int K, C, n, M;
15
16 void Build_Graph(int min_max) {
17     memset(map, 0, sizeof(map));
18     for(int i=K+1; i<=n; i++) map[0][i] = 1;
19     for(int i=1; i<=K; i++) map[i][n+1] = M;
20     for(int i=K+1; i<=n; i++) {
21         for(int j=1; j<=K; j++) {
22             if(dis[i][j]<=min_max) map[i][j] = 1;
23         }
24     }
25 }
26 bool BFS() {  // BFS构建层次网络
27     // 初始化
28     memset(used, 0, sizeof(used));
29     memset(sign, 0, sizeof(sign));
30     int queue[100*MAX] = {0};
31     queue[0] = 0;
32     used[0] = 1;
33     int t = 1, f = 0;
34     while(f<t) {
35         for(int i=0; i<=n+1; i++) {
36             if(!used[i]&&map[queue[f]][i]) {
37                 queue[t++] = i;
38                 used[i] = 1;
39                 sign[queue[f]][i] = 1;
40             }
41         }
42         f++;
43     }
44     if(used[n+1]) return true;
45     else return false;
46 }
47 int DFS(int v, int sum) {
48     int s, t;
49     if(v==n+1) return sum;
50     s = sum;
51     for(int i=0; i<=n+1; i++) {
52         if(sign[v][i]) {
53             t = DFS(i, min(map[v][i], sum));
54             map[v][i] -= t;
55             map[i][v] += t;
56             sum -= t;
57         }
58     }
59     return s-sum;
60 }
61 int main() {
62     //freopen("test.txt", "r", stdin);
63     int L, R, mid, ans;
64     while(scanf("%d %d %d", &K, &C, &M)!=EOF) {
65         n = K+C;
66         // Floyd求任意两点间的最短距离
67         for(int i=1; i<=n; i++) {
68             for(int j=1; j<=n; j++) {
69                 scanf("%d", &dis[i][j]);
70                 if(dis[i][j]==0) dis[i][j] = INF;
71             }
72         }
73         for(int k=1; k<=n; k++) {
74             for(int i=1; i<=n; i++) {
75                 if(dis[i][k]!=INF) {
76                     for(int j=1; j<=n; j++) dis[i][j] = min(dis[i][k]+dis[k][j], dis[i][j]);
77                 }
78             }
79         }
80         L = 0, R = 10000;
81
82         // 二分法搜索
83         while(L<R) {
84             mid = (L+R)>>1;
85             ans = 0;
86             // Dinic法求最大流
87             Build_Graph(mid);  // 构建容量网络(残余网络)
88             while(BFS()) ans += DFS(0, INF);  // 构建层次网络,并进行DFS增广
89             if(ans>=C) R = mid;
90             else L = mid+1;
91         }
92         printf("%d\n", R);
93     }
94     return 0;
95 }

贴板子。。。

时间: 2024-11-03 03:27:04

http://poj.org/problem?id=2112_网络流的相关文章

Roadblocks http://poj.org/problem?id=3255

Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, because she likes the scenery along the way. She has decided to take the second-shorte

poj 1651 http://poj.org/problem?id=1651

http://poj.org/problem?id=1651Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6188   Accepted: 3777 Description The multiplication puzzle is played with a row of cards, each containing a single positive integer. Dur

http://poj.org/problem?id=1330

Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17818   Accepted: 9455 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: In the figure, each

POJ3278http://poj.org/problem?id=3278

http://poj.org/problem?id=3278 题目大意: m,n两个数m可+1, -1, *2变成n,需要经过几步 #include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> #include<queue> #define max(a, b)(a > b ? a : b) #define N 100010 using namespace s

http://poj.org/problem?id=3278(bfs)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 76935   Accepted: 24323 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,00

poj 1915 http://poj.org/problem?id=1915

/**< */#include <stdio.h> #include <string.h> #include <stdlib.h> #include <queue> #include <ctype.h> #define N 310 using namespace std; int d[8][2] = {{-2, -1}, {-2, 1}, {-1, -2}, {-1, 2}, {1, -2}, {1, 2}, {2, -1}, {2, 1}

[题解]poj.org Problem#3468

Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval. In

POJ - 3436 ACM Computer Factory 网络流

POJ-3436:http://poj.org/problem?id=3436 题意 组配计算机,每个机器的能力为x,只能处理一定条件的计算机,能输出特定的计算机配置.进去的要求有1,进来的计算机这个位子就要求为1,进去的要求有0,进来的计算机这个位子就要求为0. 思路 因为点上有容量限制,所以把每个点拆掉,连一条容量为这个机器的能力的边.源点向要求为0的机器连容量inf的边,把能完全组装好计算机的机器连向汇点.中间把符合条件的机器间连边,容量为inf: #include <algorithm>

poj 1698 Alice&#39;s Chance(网络流)

Alice's Chance Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5280   Accepted: 2171 Description Alice, a charming girl, have been dreaming of being a movie star for long. Her chances will come now, for several filmmaking companies invit