POJ2455结题报告【网络流+isap+二分】

Secret Milking Machine

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6336 Accepted: 1963

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


题目大意:找出N条起点到终点的路径中最长的边的最小值,并且每条边不能重复走。

解题思路:整体思路是二分求解,删去大于mid的边,然后求最大流,判断最大流是否大于等于N,即找到了N条或N条以上的路径。
在题目中边是双向的,这样就需要建立双向边,但是还有可能出个问题,就是建立双向边就不能保证此边只走一次,因为可能从另一个方向返回。如果存在那样的反例,那么这样建边就是错的,不过其实反例不存在的。如果存在某条边E(i,j)被重复走了,那就说明在这条边上存在两条路径,即S-E(S,k1)-...-E(kp,i)-E(i,j,)-E(j,kq)...-E(kn,T)-T和S-E(S,h1)-...E(hp,j)-E(j,i)-E(i,hq)...E(hn,T)-T,如果这条路径中除了E(i,j)外其它边都是被走了一次的,那么我们则可以得到两条不经过E(i,j)的路径, S-E(S,k1)-...-E(kp,i)-E(i,hq)...E(hn,T)-T和S-E(S,h1)-...E(hp,j)-E(j,kq)...-E(kn,T)-T。那么可以证明:对于每组重复走了的某条边的两条路径,存在两条路径可以“绕过”该边。那么回到这道题,建立两条双向边是没有问题的。结果Accept了,也说明是正解。


代码:
#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<map>
#include<string>

//#define r(x,date) (*((STRUCT*)x).date)
//#define dp(i,j) dp[i][j]
//#define dp(i,j,k) dp[i][j][k]
#define INF 0x7ffffff
#define ff(i,from,to) for(i=from;i<=to;i++)
#define rff(i,from,to) for(i=from;i>=to;i--)
#define ll long long
#define MAX_SIDE 160100
#define MAX_NODE 210
#define mset(a,num) memset(a,num,sizeof(a))
using namespace std;

template<class T>bool myfun_upmax(T &a,const T&b){return b>a?a=b,1:0;}
template<class T>bool myfun_upmin(T &a,const T&b){return b<a?a=b,1:0;}
template<class T>void myfun_swap(T &a,T &b){T temp;temp=a;a=b;b=temp;}
template<class T>T myfun_max(const T a,const T b){return a>b?a:b;}
template<class T>T myfun_min(const T a,const T b){return a<b?a:b;}


int cmp(const void *a,const void *b){
    return 0;
}

inline void readint(int &ret) {
	char c;
	do {	c = getchar();
	} while(c < '0' || c > '9');
	ret = c - '0';
	while((c=getchar()) >= '0' && c <= '9')
		ret = ret * 10 + ( c - '0' );
}

int node[MAX_NODE];
struct SIDE{
    int to,next,c,l;
    SIDE(){}
    SIDE(int to,int next,int c,int l):to(to),next(next),c(c),l(l){}
}side[MAX_SIDE];

int top;
void add_side(int u,int v,int c,int l){
    side[top]=SIDE(v,node[u],c,l);
    node[u]=top++;
    //side[top]=SIDE(u,node[v],0,l);
    //node[v]=top++;
}

int START,END,SIZE;
int pre[MAX_NODE],cur[MAX_NODE],dis[MAX_NODE],gap[MAX_NODE];
int sap(int limit){
    int maxflow=0;
    int aug=INF;
    int u=START,v;
    for(int i=0;i<SIZE;i++){
        dis[i]=gap[i]=0;
        cur[i]=node[i];
    }
    gap[START]=SIZE;
    pre[START]=START;
    while(dis[u]<SIZE){
        for(int &i=cur[u];i!=-1;i=side[i].next){
            v=side[i].to;
            if(side[i].c && side[i].l<=limit && dis[u]==dis[v]+1)break;
        }
        if(cur[u]!=-1){
            myfun_upmin(aug,side[cur[u]].c);
            pre[v]=u;u=v;
            if(u==END){
                maxflow+=aug;
                while(u!=START){
                    u=pre[u];
                    side[cur[u]].c-=aug;
                    //side[cur[u]^1].c+=aug;
                }
                aug=INF;
            }
        }else{
            int mindis=SIZE;
            for(int i=node[u];i!=-1;i=side[i].next){
                v=side[i].to;
                if(side[i].c && side[i].l<=limit && myfun_upmin(mindis,dis[v]))
                    cur[u]=i;
            }
            if(--gap[dis[u]]==0)break;
            ++gap[dis[u]=mindis+1];
            u=pre[u];
        }
    }
    return maxflow;
}

void reset(){
    for(int i=0;i<top;i++){
        side[i].c=1;
    }
}

int T;
int solve(int low,int high){
    while(low<=high){
        int mid=low+(high-low)/2;
        reset();
        if(sap(mid)>=T)high=mid-1;
        else low=mid+1;
    }
    return low;
}

int main()
{
    int N,P;
    readint(N);
    readint(P);
    readint(T);
    int maxside=0;
    START=1;
    END=N;
    SIZE=N+1;
    top=0;
    memset(node,-1,sizeof(node));
    while(P--){
        int u,v,l;
        readint(u);
        readint(v);
        readint(l);
        add_side(u,v,1,l);
        add_side(v,u,1,l);
        myfun_upmax(maxside,l);
    }
    printf("%d\n",solve(1,maxside));
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1011 1012 1013 1014 1015 1017 1018 1019 1028 1032 1035 1040 1042 1045 1046 1047 1050 1056 1061 1062 1063 1065 1067 1068 1080 1083 1088 1089 1091 1094 1095 1102 1111 1113 1117 1118 1125 1126 1127 1129 1130 1131 1132 1141 1142 1144 1149 1151 1157 1159 1160 1163 1164 1166 1174 1177 1182 1183 1186 1188 1189 1190 1191 1195 1200 1201 1207 1218 1226 1251 1256 1258 1260 1273 1274 1276 1283 1298 1305 1306 1308 1315 1316 1319 1321 1323 1324 1325 1328 1338 1339 1364 1389 1401 1422 1423 1426 1455 1458 1459 1469 1477 1485 1511 1517 1519 1523 1552 1562 1564 1565 1573 1579 1651 1654 1655 1656 1658 1659 1663 1664 1699 1700 1703 1716 1730 1737 1740 1753 1771 1797 1799 1804 1833 1837 1840 1844 1861 1887 1906 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1970 1979 1980 1985 1988 1989 2000 2001 2002 2018 2019 2021 2027 2033 2044 2051 2081 2084 2104 2109 2112 2135 2136 2137 2153 2155 2181 2182 2184 2185 2186 2187 2188 2190 2192 2195 2228 2229 2234 2236 2241 2242 2245 2247 2248 2249 2253 2264 2287 2299 2301 2309 2336 2337 2348 2352 2353 2362 2371 2378 2386 2387 2388 2389 2392 2393 2394 2402 2403 2406 2411 2413 2419 2421 2446 2449 2456 2479 2488 2492 2503 2509 2513 2524 2528 2531 2533 2545 2553 2559 2564 2575 2576 2586 2591 2593 2594 2602 2623 2632 2656 2676 2680 2707 2750 2774 2777 2780 2782 2812 2818 2840 2908 2922 2934 2965 2983 2993 2996 3020 3041 3168 3169 3176 3183 3184 3185 3191 3193 3214 3219 3224 3250 3253 3255 3256 3257 3258 3259 3264 3267 3273 3275 3277 3278 3279 3280 3295 3297 3302 3303 3311 3312 3321 3325 3348 3349 3355 3356 3357 3368 3372 3386 3400 3421 3424 3425 3427 3428 3438 3452 3468 3486 3517 3561 3585 3589 3602 3612 3614 3615 3616 3617 3618 3619 3620 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3637 3660 3661 3662 3663 3664 3665 3666 3668 3669 3670 3671 3672 3673 3687

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值