poj 2455 Secret Milking Machine 从1到n有多少条不同的路径(每条边走一次) 网络流 cap赋值为1
2011-09-19 19:15
435 查看
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
//
有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
;
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
,cur
,dis
,gap
;
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;
}
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
//
有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
;
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
,cur
,dis
,gap
;
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;
}
相关文章推荐
- POJ 2455 Secret Milking Machine 网络流初步 Dinic
- 【网络流】 POJ 2455 Secret Milking Machine
- POJ 2455 Secret Milking Machine(搜索-二分,网络流-最大流)
- POJ 2455 Secret Milking Machine(搜索-二分,网络流-最大流)
- poj 2455 Secret Milking Machine 【二分 + 最大流】 【1到N不重复路径不少于T条时,求被选中路径上的最大边权值 的最小值】
- 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
- POJ2455 Secret Milking Machine【二分,最大流】
- poj2455 Secret Milking Machine(二分答案+最大流)
- poj 2455 Secret Milking Machine
- poj 2455 Secret Milking Machine 最大流+二分查找
- POJ 2455 Secret Milking Machine && 二分枚举 + 最大流
- POJ2455 Secret Milking Machine 典型二分答案+最大流判定
- POJ2455 Secret Milking Machine ——二分答案+网络流——Pku2455
- POJ 2455 Secret Milking Machine (最大流)
- POJ2455 Secret Milking Machine 典型二分答案+最大流判定
- poj 2455 Secret Milking Machine