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.
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.
* 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.
Huge input data,scanf is recommended.
题意:
有一个起点,一个终点,找出T条不含相同路径的路线,而且其最长边最短.
思路:
这题运用到了最大流,做题首先要懂捉取关键字,不同,其中,最短,所以重其中这个字眼,我们很容易想到二分答案,那我们怎么知道这个mid是否符合呢,只要我们使其中小于等于它的边都构成路径,而且能找到>=P条不相交的,明显这个mid是符合的,而且学过最大流知道,最大流一般用于图的假设是否成立,那我们怎样用呢,我们可以在建图的时候,符合<=mid的我们就设为1,看最后到终点的流就知道有多少不相关联的路径.
AC code:
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
#define INF 2147483647
vector<int>g[410];
queue<int>q;
struct Edge {
int to, from, cap, flow;
}edge[160000];
struct node {
int a;
int b;
int time;
}arr[40010];
int s, t, n, cnt, x, y, sum, w, d, cur[410], T, dist[410],m;
bool vis[410];
void addedge(int x, int y, int cap) {
edge[cnt++] = Edge { y, x, cap, 0 };
edge[cnt++] = Edge { x, y, cap, 0 };
g[x].push_back(cnt - 2);
g[y].push_back(cnt - 1);
}
bool bfs() {
memset(vis, 0, sizeof(vis));
vis[s] = 1;
dist[s] = 0;
q.push(s);
while (!q.empty()) {
int x = q.front(); q.pop();
for (int i = 0; i<g[x].size(); i++) {
Edge& e = edge[g[x][i]];
if (e.cap>e.flow && !vis[e.to]) {
vis[e.to] = 1;
dist[e.to] = dist[x] + 1;
q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int x, int a) {
if (x == t || !a)return a;
int flow = 0, f;
for (int& i = cur[x]; i<g[x].size(); i++) {
Edge& e = edge[g[x][i]];
if (dist[x] + 1 == dist[e.to] && (f = dfs(e.to, min(a, e.cap - e.flow)))) {
e.flow += f;
edge[g[x][i] ^ 1].flow -= f;
flow += f;
a -= f;
if (!a)break;
}
}
return flow;
}
int maxflow() {
int flow = 0;
while (bfs()) {
memset(cur, 0, sizeof(cur));
flow += dfs(s, INF);
}
return flow;
}
void input()
{
cin >> n >> m >> T;
s = 1, t = n;
for (int i = 1; i <= m; i++)
{
scanf("%d%d%d", &arr[i].a, &arr[i].b, &arr[i].time);
}
}
void bulid(int x)
{
cnt = 0;
for (int i = 1; i <= 400; i++)
g[i].clear();
for (int i = 1; i <= m; i++)
{
if (arr[i].time <= x)
addedge(arr[i].a, arr[i].b, 1);
}
}
int find()
{
int ans;
int l = 1, r = 1000000;
while (l <= r)
{
int mid =(r+l) / 2;
bulid(mid);
if (maxflow() >= T)r = mid-1,ans=mid;
else l = mid+1;
}
return ans;
}
int main()
{
input();
cout << find() << endl;
return 0;
}
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
#define INF 2147483647
vector<int>g[410];
queue<int>q;
struct Edge {
int to, from, cap, flow;
}edge[160000];
struct node {
int a;
int b;
int time;
}arr[40010];
int s, t, n, cnt, x, y, sum, w, d, cur[410], T, dist[410],m;
bool vis[410];
void addedge(int x, int y, int cap) {
edge[cnt++] = Edge { y, x, cap, 0 };
edge[cnt++] = Edge { x, y, cap, 0 };
g[x].push_back(cnt - 2);
g[y].push_back(cnt - 1);
}
bool bfs() {
memset(vis, 0, sizeof(vis));
vis[s] = 1;
dist[s] = 0;
q.push(s);
while (!q.empty()) {
int x = q.front(); q.pop();
for (int i = 0; i<g[x].size(); i++) {
Edge& e = edge[g[x][i]];
if (e.cap>e.flow && !vis[e.to]) {
vis[e.to] = 1;
dist[e.to] = dist[x] + 1;
q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int x, int a) {
if (x == t || !a)return a;
int flow = 0, f;
for (int& i = cur[x]; i<g[x].size(); i++) {
Edge& e = edge[g[x][i]];
if (dist[x] + 1 == dist[e.to] && (f = dfs(e.to, min(a, e.cap - e.flow)))) {
e.flow += f;
edge[g[x][i] ^ 1].flow -= f;
flow += f;
a -= f;
if (!a)break;
}
}
return flow;
}
int maxflow() {
int flow = 0;
while (bfs()) {
memset(cur, 0, sizeof(cur));
flow += dfs(s, INF);
}
return flow;
}
void input()
{
cin >> n >> m >> T;
s = 1, t = n;
for (int i = 1; i <= m; i++)
{
scanf("%d%d%d", &arr[i].a, &arr[i].b, &arr[i].time);
}
}
void bulid(int x)
{
cnt = 0;
for (int i = 1; i <= 400; i++)
g[i].clear();
for (int i = 1; i <= m; i++)
{
if (arr[i].time <= x)
addedge(arr[i].a, arr[i].b, 1);
}
}
int find()
{
int ans;
int l = 1, r = 1000000;
while (l <= r)
{
int mid =(r+l) / 2;
bulid(mid);
if (maxflow() >= T)r = mid-1,ans=mid;
else l = mid+1;
}
return ans;
}
int main()
{
input();
cout << find() << endl;
return 0;
}