nyoj 310二分+dinic
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
#define inf 0x7fffffff
#define N 300
int p,t,n;
int Min(int a,int b) {
return a>b?b:a;
}
struct node {
int u,v,w,f;
}mp[40001];
int flow[N][N],d[N];
int bfs() {
int cur,i;
queue<int>q;
memset(d,0,sizeof(d));
d[1]=1;
q.push(1);
while(!q.empty()) {
cur=q.front();
q.pop();
for(i=1;i<=n;i++)
if(flow[cur][i]&&!d[i]) {
d[i]=d[cur]+1;
if(i==n)
return 1;
q.push(i);
}
}
return 0;
}
int dfs(int u,int limit) {
if(u==n)
return limit;
int cost=0,i,k;
for(i=1;i<=n;i++)
if(d[i]==d[u]+1&&flow[u][i]) {
k=dfs(i,Min(limit-cost,flow[u][i]));
if(k>0) {
flow[u][i]-=k;
flow[i][u]+=k;
cost+=k;
if(cost==limit)
break;
}
else
d[i]=-1;
}
return cost;
}
int dinic (int mid) {
int ans=0,i;
memset(flow,0,sizeof(flow));
for(i=1;i<=p;i++)
if(mp[i].w<=mid) {
flow[mp[i].u][mp[i].v]++;
flow[mp[i].v][mp[i].u]++;
}
while(bfs())
ans+=dfs(1,inf);
if(ans>=t)
return 1;
return 0;
}
int main() {
int i,min,max,mid;
while(scanf("%d%d%d",&n,&p,&t)!=EOF) {
min=inf;
max=-1;
for(i=1;i<=p;i++) {
scanf("%d%d%d",&mp[i].u,&mp[i].v,&mp[i].w);
if(mp[i].w>max)
max=mp[i].w;
if(mp[i].w<min)
min=mp[i].w;
}
while(min<=max) {
mid=(min+max)/2;
if(dinic(mid))
max=mid-1;
else
min=mid+1;
}
printf("%d\n",min);
}
return 0;
}
#include<queue>
#include<string.h>
using namespace std;
#define inf 0x7fffffff
#define N 300
int p,t,n;
int Min(int a,int b) {
return a>b?b:a;
}
struct node {
int u,v,w,f;
}mp[40001];
int flow[N][N],d[N];
int bfs() {
int cur,i;
queue<int>q;
memset(d,0,sizeof(d));
d[1]=1;
q.push(1);
while(!q.empty()) {
cur=q.front();
q.pop();
for(i=1;i<=n;i++)
if(flow[cur][i]&&!d[i]) {
d[i]=d[cur]+1;
if(i==n)
return 1;
q.push(i);
}
}
return 0;
}
int dfs(int u,int limit) {
if(u==n)
return limit;
int cost=0,i,k;
for(i=1;i<=n;i++)
if(d[i]==d[u]+1&&flow[u][i]) {
k=dfs(i,Min(limit-cost,flow[u][i]));
if(k>0) {
flow[u][i]-=k;
flow[i][u]+=k;
cost+=k;
if(cost==limit)
break;
}
else
d[i]=-1;
}
return cost;
}
int dinic (int mid) {
int ans=0,i;
memset(flow,0,sizeof(flow));
for(i=1;i<=p;i++)
if(mp[i].w<=mid) {
flow[mp[i].u][mp[i].v]++;
flow[mp[i].v][mp[i].u]++;
}
while(bfs())
ans+=dfs(1,inf);
if(ans>=t)
return 1;
return 0;
}
int main() {
int i,min,max,mid;
while(scanf("%d%d%d",&n,&p,&t)!=EOF) {
min=inf;
max=-1;
for(i=1;i<=p;i++) {
scanf("%d%d%d",&mp[i].u,&mp[i].v,&mp[i].w);
if(mp[i].w>max)
max=mp[i].w;
if(mp[i].w<min)
min=mp[i].w;
}
while(min<=max) {
mid=(min+max)/2;
if(dinic(mid))
max=mid-1;
else
min=mid+1;
}
printf("%d\n",min);
}
return 0;
}