[POJ 2455] Secret Milking Machine【二分答案+网络流】

  • 2018-01-18
  • 0
  • 0

Problem:

Time Limit: 1000MS Memory Limit: 65536K

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

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

Solution:

这又是一道二分答案+网络流判可行性的模板题,然而悲催的我把读入优化打错了 QAQ,结果 getchar() 读不到数据,一直处于等待状态,然后就 TLE 掉了。。

由于需要求的是最短的单段路径长度,我们可以二分答案,将小于等于该长度的路径连上一条容量为 1 的边,表示可以通过,然后跑 Dinic 最大流即可(ISAP 不稳定,似乎会被本题数据卡掉)。而由于容量总是 1 的特点,本题使用邻接矩阵存储也许会比邻接表快,但注意容量是累加的,而不是取较小值,详见代码。

虽然时间复杂度较高,但 Dinic 网络流很难达到最坏情况,所以实际效率并不低。

Code: O(N4logN) [1284K, 219MS]

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

inline void getint(int &num){
	char ch;
	while(!isdigit(ch = getchar()));
	num = ch - '0';
	while(isdigit(ch = getchar())) num = (num << 1) + (num << 3) + ch - '0';
}

inline int min(const int &a, const int &b) {return a < b ? a : b;}

int N, P, T;
int cap[205][205];

struct Trail{
	int s, t, l;
	
	inline bool operator < (const Trail &ed2) const {return l < ed2.l;}
} ed[40005];

int q[205], fr, re;
int lev[205], arc[205];

inline bool Dinic_BFS(){
	memset(lev, -1, sizeof(lev));
	fr = re = 0, q[re++] = 1, lev[1] = 0;
	while(fr != re){
		int u = q[fr++];
		for(register int i = 1; i <= N; i++)
			if(cap[u][i] && lev[i] < 0)
				lev[i] = lev[u] + 1, q[re++] = i;
	}
	return lev[N] > -1;
}

inline int Dinic_DFS(int u, int curflow){
	if(u == N) return curflow;
	int remflow = curflow;
	for(register int i = arc[u]; i <= N; i++)
		if(cap[u][i]){
			arc[u] = i;
			if(lev[i] == lev[u] + 1){
				int dflow = Dinic_DFS(i, min(remflow, cap[u][i]));
				if(dflow){
					cap[u][i] -= dflow, cap[i][u] += dflow;
					remflow -= dflow;
				}
			}
		}
	return curflow - remflow;
}

inline int Dinic(){
	int maxflow = 0;
	while(Dinic_BFS()){
		for(register int i = 1; i <= N; i++) arc[i] = 1;
		maxflow += Dinic_DFS(1, 0x3f3f3f3f);
	}
	return maxflow;
}

inline bool check(int keyid){
	memset(cap, 0, sizeof(cap));
	for(register int i = 1; i <= keyid; i++)
		cap[ed[i].s][ed[i].t]++, cap[ed[i].t][ed[i].s]++;
	int maxflow = Dinic();
	return maxflow >= T;
}

int main(){
	getint(N), getint(P), getint(T);
	for(register int i = 1; i <= P; i++)
		getint(ed[i].s), getint(ed[i].t), getint(ed[i].l);
	sort(ed + 1, ed + P + 1);
	int lft = 1, rt = P;
	while(lft < rt){
		int mid = lft + rt >> 1;
		if(check(mid)) rt = mid;
		else lft = mid + 1;
	}
	printf("%d\n", ed[lft].l);
	return 0;
}

评论

还没有任何评论,你来说两句吧



常年不在线的QQ:
49750

不定期更新的GitHub:
https://github.com/Darkleafin


OPEN AT 2017.12.10

如遇到代码不能正常显示的情况,请刷新页面。
If the code cannot be displayed normally, please refresh the page.


发现一个优美的网站:
https://visualgo.net/en
















- Theme by Qzhai