Description
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
* 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
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
//
有N个点,之间有P条路(双向),要求每次从1到N走不同的路T次,求这T次中两点间路最长的一条
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1010;
const int M=500000;
const int inf=(1<<28);
int head[N];
struct Edge
{
int v,next,w;
int pw;//原图中u->v的边权
} edge[M];
int cnt,n,s,t;//n从0开始 0->n-1
void addedge(int u,int v,int w)
{
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].pw=w;
edge[cnt].next=head[u];
head[u]=cnt++;
edge[cnt].v=u;
edge[cnt].w=0;
edge[cnt].pw=w;
edge[cnt].next=head[v];
head[v]=cnt++;
}
int sap()
{
int pre[N],cur[N],dis[N],gap[N];
int flow=0,aug=inf,u;
bool flag;
for(int i=0; i<n; i++)
{
cur[i]=head[i];
gap[i]=dis[i]=0;
}
gap[s]=n;
u=pre[s]=s;
while(dis[s]<n)
{
flag=0;
for(int &j=cur[u]; j!=-1; j=edge[j].next)
{
int v=edge[j].v;
if(edge[j].w>0&&dis[u]==dis[v]+1)
{
flag=1;
if(edge[j].w<aug) aug=edge[j].w;
pre[v]=u;
u=v;
if(u==t)
{
flow+=aug;
while(u!=s)
{
u=pre[u];
edge[cur[u]].w-=aug;
edge[cur[u]^1].w+=aug;
}
aug=inf;
}
break;
}
}
if(flag) continue;
int mindis=n;
for(int j=head[u]; j!=-1; j=edge[j].next)
{
int v=edge[j].v;
if(edge[j].w>0&&dis[v]<mindis)
{
mindis=dis[v];
cur[u]=j;
}
}
if((--gap[dis[u]])==0)
break;
gap[dis[u]=mindis+1]++;
u=pre[u];
}
return flow;
}
//初始化 cnt=0;memset(head,-1,sizeof(head));
//从1到n有多少条不同的路径(每条边走一次) 网络流 cap赋值为1
int main()
{
int p,limit;
while(scanf("%d%d%d",&n,&p,&limit)==3)
{
s=0,t=n-1;
cnt=0;
memset(head,-1,sizeof(head));
int l=inf,r=0;
for(int i=0;i<p;i++)
{
int x,y,c;scanf("%d%d%d",&x,&y,&c);x--,y--;
addedge(x,y,c);
addedge(y,x,c);
r=max(r,c);l=min(l,c);
}
while(l<r)
{
int mid=(l+r)>>1;
for(int i=0;i<cnt;i++)
{
if(i&1)//反向边
{
edge[i].w=0;
}
else//正向边
{
if(edge[i].pw<=mid) edge[i].w=1;//网络流 边容量赋值
else edge[i].w=0;
}
}
int ans=sap();
if(ans>=limit) r=mid;
else l=mid+1;
}
printf("%d\n",r);
}
return 0;
}
本文介绍了一个基于最短路径算法的问题,即如何在限定条件下找到多次从起点到终点的路径,使得所使用的路径中最长的边尽可能短。文章通过具体实例展示了如何使用网络流算法解决这一问题,并提供了一段实现该算法的C++代码。

被折叠的 条评论
为什么被折叠?



