POJ2455-Secret Milking Machine
2017-08-28 16:56
197 查看
Secret Milking Machine
Description
Farmer John is constructing a new milking machine and wishes to keep it secret as long as possible. He has hidden in it deep within his farm and needs to be able to get to the machine without being detected. He must make a total of T (1 <= T <= 200) trips to
the machine during its construction. He has a secret tunnel that he uses only for the return trips.
The farm comprises N (2 <= N <= 200) landmarks (numbered 1..N) connected by P (1 <= P <= 40,000) bidirectional trails (numbered 1..P) and with a positive length that does not exceed 1,000,000. Multiple trails might join a pair of landmarks.
To minimize his chances of detection, FJ knows he cannot use any trail on the farm more than once and that he should try to use the shortest trails.
Help FJ get from the barn (landmark 1) to the secret milking machine (landmark N) a total of T times. Find the minimum possible length of the longest single trail that he will have to use, subject to the constraint that he use no trail more than once. (Note
well: The goal is to minimize the length of the longest trail, not the sum of the trail lengths.)
It is guaranteed that FJ can make all T trips without reusing a trail.
Input
* Line 1: Three space-separated integers: N, P, and T
* Lines 2..P+1: Line i+1 contains three space-separated integers, A_i, B_i, and L_i, indicating that a trail connects landmark A_i to landmark B_i with length L_i.
Output
* Line 1: A single integer that is the minimum possible length of the longest segment of Farmer John's route.
Sample Input
Sample Output
Hint
Farmer John can travel trails 1 - 2 - 3 - 7 and 1 - 6 - 7. None of the trails travelled exceeds 5 units in length. It is impossible for Farmer John to travel from 1 to 7 twice without using at least one trail of length 5.
Huge input data,scanf is recommended.
Source
USACO 2005 February Gold
题意:给你一个n个点和m条边的无向图以及每条边的权值。要求从1到n至少要有T条边不重复的路径,在这个前提下求出所有路径最大边权值的最小值
解题思路:二分+网络流
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
#include <bitset>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
#define MAXN 500
int n,m,t;
int x[4 * MAXN*MAXN], y[4 * MAXN*MAXN], w[4 * MAXN*MAXN];
struct node
{
int u, v, next, cap;
} edge[4*MAXN*MAXN];
int s[MAXN], d[MAXN], visit[MAXN];
int cnt;
void init()
{
cnt = 0;
memset(s, -1, sizeof(s));
}
void add(int u, int v, int c)
{
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].cap = c;
edge[cnt].next = s[u];
s[u] = cnt++;
edge[cnt].u = v;
edge[cnt].v = u;
edge[cnt].cap = c;//无向边cap不为零
edge[cnt].next = s[v];
s[v] = cnt++;
}
bool BFS(int ss, int ee)
{
memset(d, 0, sizeof d);
d[ss] = 1;
queue<int>q;
q.push(ss);
while (!q.empty())
{
int pre = q.front();
q.pop();
for (int i = s[pre]; ~i; i = edge[i].next)
{
int v = edge[i].v;
if (edge[i].cap > 0 && !d[v])
{
d[v] = d[pre] + 1;
q.push(v);
}
}
}
return d[ee];
}
int DFS(int x, int exp, int ee)
{
if (x == ee || !exp) return exp;
int temp, flow = 0;
for (int i = s[x]; ~i; i = edge[i].next)
{
int v = edge[i].v;
if (d[v] == d[x] + 1 && (temp = (DFS(v, min(exp, edge[i].cap), ee))) > 0)
{
edge[i].cap -= temp;
edge[i ^ 1].cap += temp;
flow += temp;
exp -= temp;
if (!exp) break;
}
}
if (!flow) d[x] = 0;
return flow;
}
int Dinic_flow(int d)
{
init();
add(n + 1, 1, INF);
add(n, n + 2, INF);
for (int i = 0; i < m; i++)
if (w[i] <= d) add(x[i], y[i], 1);
int ans = 0;
while (BFS(n+1, n+2))
ans += DFS(n+1, INF, n+2);
return ans;
}
int main()
{
while (~scanf("%d %d %d", &n, &m, &t))
{
int ma = -1;
for (int i = 0; i < m; i++)
{
scanf("%d%d%d", &x[i], &y[i], &w[i]);
ma = max(ma, w[i]);
}
int l = 0, r = ma, ans;
while (l <= r)
{
int mid = (l + r) >> 1;
if (Dinic_flow(mid) >= t) { ans = mid; r = mid - 1; }
else l = mid + 1;
}
printf("%d\n", ans);
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12405 | Accepted: 3623 |
Farmer John is constructing a new milking machine and wishes to keep it secret as long as possible. He has hidden in it deep within his farm and needs to be able to get to the machine without being detected. He must make a total of T (1 <= T <= 200) trips to
the machine during its construction. He has a secret tunnel that he uses only for the return trips.
The farm comprises N (2 <= N <= 200) landmarks (numbered 1..N) connected by P (1 <= P <= 40,000) bidirectional trails (numbered 1..P) and with a positive length that does not exceed 1,000,000. Multiple trails might join a pair of landmarks.
To minimize his chances of detection, FJ knows he cannot use any trail on the farm more than once and that he should try to use the shortest trails.
Help FJ get from the barn (landmark 1) to the secret milking machine (landmark N) a total of T times. Find the minimum possible length of the longest single trail that he will have to use, subject to the constraint that he use no trail more than once. (Note
well: The goal is to minimize the length of the longest trail, not the sum of the trail lengths.)
It is guaranteed that FJ can make all T trips without reusing a trail.
Input
* Line 1: Three space-separated integers: N, P, and T
* Lines 2..P+1: Line i+1 contains three space-separated integers, A_i, B_i, and L_i, indicating that a trail connects landmark A_i to landmark B_i with length L_i.
Output
* Line 1: A single integer that is the minimum possible length of the longest segment of Farmer John's route.
Sample Input
7 9 2 1 2 2 2 3 5 3 7 5 1 4 1 4 3 1 4 5 7 5 7 1 1 6 3 6 7 3
Sample Output
5
Hint
Farmer John can travel trails 1 - 2 - 3 - 7 and 1 - 6 - 7. None of the trails travelled exceeds 5 units in length. It is impossible for Farmer John to travel from 1 to 7 twice without using at least one trail of length 5.
Huge input data,scanf is recommended.
Source
USACO 2005 February Gold
题意:给你一个n个点和m条边的无向图以及每条边的权值。要求从1到n至少要有T条边不重复的路径,在这个前提下求出所有路径最大边权值的最小值
解题思路:二分+网络流
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
#include <bitset>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
#define MAXN 500
int n,m,t;
int x[4 * MAXN*MAXN], y[4 * MAXN*MAXN], w[4 * MAXN*MAXN];
struct node
{
int u, v, next, cap;
} edge[4*MAXN*MAXN];
int s[MAXN], d[MAXN], visit[MAXN];
int cnt;
void init()
{
cnt = 0;
memset(s, -1, sizeof(s));
}
void add(int u, int v, int c)
{
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].cap = c;
edge[cnt].next = s[u];
s[u] = cnt++;
edge[cnt].u = v;
edge[cnt].v = u;
edge[cnt].cap = c;//无向边cap不为零
edge[cnt].next = s[v];
s[v] = cnt++;
}
bool BFS(int ss, int ee)
{
memset(d, 0, sizeof d);
d[ss] = 1;
queue<int>q;
q.push(ss);
while (!q.empty())
{
int pre = q.front();
q.pop();
for (int i = s[pre]; ~i; i = edge[i].next)
{
int v = edge[i].v;
if (edge[i].cap > 0 && !d[v])
{
d[v] = d[pre] + 1;
q.push(v);
}
}
}
return d[ee];
}
int DFS(int x, int exp, int ee)
{
if (x == ee || !exp) return exp;
int temp, flow = 0;
for (int i = s[x]; ~i; i = edge[i].next)
{
int v = edge[i].v;
if (d[v] == d[x] + 1 && (temp = (DFS(v, min(exp, edge[i].cap), ee))) > 0)
{
edge[i].cap -= temp;
edge[i ^ 1].cap += temp;
flow += temp;
exp -= temp;
if (!exp) break;
}
}
if (!flow) d[x] = 0;
return flow;
}
int Dinic_flow(int d)
{
init();
add(n + 1, 1, INF);
add(n, n + 2, INF);
for (int i = 0; i < m; i++)
if (w[i] <= d) add(x[i], y[i], 1);
int ans = 0;
while (BFS(n+1, n+2))
ans += DFS(n+1, INF, n+2);
return ans;
}
int main()
{
while (~scanf("%d %d %d", &n, &m, &t))
{
int ma = -1;
for (int i = 0; i < m; i++)
{
scanf("%d%d%d", &x[i], &y[i], &w[i]);
ma = max(ma, w[i]);
}
int l = 0, r = ma, ans;
while (l <= r)
{
int mid = (l + r) >> 1;
if (Dinic_flow(mid) >= t) { ans = mid; r = mid - 1; }
else l = mid + 1;
}
printf("%d\n", ans);
}
return 0;
}
相关文章推荐
- 解题报告 之 POJ2455 Secret Milking Machine
- 【poj2455】 Secret Milking Machine
- POJ2455 Secret Milking Machine
- poj2455 Secret Milking Machine
- POJ2455 Secret Milking Machine(二分上线构图求最大流)
- POJ2455 Secret Milking Machine ——二分答案+网络流
- POJ2455 Secret Milking Machine ——二分答案+网络流——Pku2455
- POJ 2455 Secret Milking Machine 最大流建模
- BZOJ 1733: [Usaco2005 feb]Secret Milking Machine 神秘的挤奶机
- poj 2455 Secret Milking Machine 二分+最大流
- 【bzoj1733】[Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 二分+网络流最大流
- bzoj 1733: [Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 二分+最大流
- 【最大流+dinic+二分】北大 poj 2455 Secret Milking Machine
- POJ 2455 Secret Milking Machine
- POJ 2455 Secret Milking Machine (最大流)
- POJ-2455-Secret Milking Machine
- POJ 2455 Secret Milking Machine(搜索-二分,网络流-最大流)
- POJ 2455 Secret Milking Machine 网络流初步 Dinic
- poj 2455 Secret Milking Machine
- BZOJ1733: [Usaco2005 feb]Secret Milking Machine 神秘的挤奶机