【poj2455】 Secret Milking Machine
http://poj.org/problem?id=2455 (题目链接)
题意
给出一张n个点,p条边的无向图,需要从1号节点走到n号节点一共T次,每条边只能经过1次,问T次经过的最大的边最小是多少。
Solution
很显然,二分答案,然后建图跑最大流即可。
细节
双向边?
代码
// poj2455 #include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<cmath> #include<queue> #define LL long long #define inf 2147483640 #define Pi acos(-1.0) #define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout); using namespace std; const int maxn=300,maxm=40010; struct edge {int to,next,w;}e[maxm<<1]; int head[maxn],d[maxn]; int cnt,n,p,T,u[maxm],v[maxm],w[maxm],ans; void link(int u,int v,int w) { e[++cnt]=(edge){v,head[u],w};head[u]=cnt; e[++cnt]=(edge){u,head[v],w};head[v]=cnt; } void build(int k) { cnt=1; memset(head,0,sizeof(head)); for (int i=1;i<=p;i++) if (w[i]<=k) link(u[i],v[i],1); } bool bfs() { memset(d,-1,sizeof(d)); queue<int> q;q.push(1);d[1]=0; while (!q.empty()) { int x=q.front();q.pop(); for (int i=head[x];i;i=e[i].next) if (e[i].w && d[e[i].to]<0) { d[e[i].to]=d[x]+1; q.push(e[i].to); } } return d[n]>0; } int dfs(int x,int f) { if (x==n || f==0) return f; int used=0,w; for (int i=head[x];i;i=e[i].next) if (e[i].w && d[e[i].to]==d[x]+1) { w=dfs(e[i].to,min(e[i].w,f-used)); used+=w; e[i].w-=w;e[i^1].w+=w; if (used==f) return used; } if (!used) d[x]=-1; return used; } void Dinic() { while (bfs()) ans+=dfs(1,inf); } int main() { scanf("%d%d%d",&n,&p,&T); int L=inf,R=0; for (int i=1;i<=p;i++) scanf("%d%d%d",&u[i],&v[i],&w[i]),L=min(L,w[i]),R=max(R,w[i]); int res=0; while (L<=R) { int mid=(L+R)>>1; build(mid);ans=0; Dinic(); if (ans>=T) res=mid,R=mid-1; else L=mid+1; } printf("%d",res); return 0; }
This passage is made by MashiroSky.