题意
N(2<=n<=200)个城市,M(1<=m<=40000)条无向边,你要找T(1<=T<=200)条从城市1到城市N的路,使得最长的边的长度最小,边不能重复用。
题解
简单的网络流判定。
一看问法想到二分答案。然后边不能重复直接上网络流。
(用边长小于mid的边建图然后跑最大流,最后比较流量和k)
(然而不知为何写挂了)
#include<cstdio>
#include<cstring>
#include<queue>
#define find_min(a,b) a<b?a:b
#define find_max(a,b) a>b?a:b using namespace std; const int N = ;
const int MAX = (int)10e7; struct Edge{
int s,e,v,next;
}edge[*N*N],mat[N*N]; int e_num,head[N],d[N],sp,tp;
int n,m; void AddEdge(int a,int b,int c){
edge[e_num].s=a; edge[e_num].e=b; edge[e_num].v=c;
edge[e_num].next=head[a]; head[a]=e_num++; edge[e_num].s=b; edge[e_num].e=a; edge[e_num].v=c;
edge[e_num].next=head[b]; head[b]=e_num++;
} int bfs(){
queue <int> q;
memset(d,-,sizeof(d));
d[sp]=;
q.push(sp);
while(!q.empty()){
int cur=q.front();
q.pop();
for(int i=head[cur];i!=-;i=edge[i].next){
int u=edge[i].e;
if(d[u]==- && edge[i].v>){
d[u]=d[cur]+;
q.push(u);
}
}
}
return d[tp] != -;
} int dfs(int a,int b){
int r=;
if(a==tp)return b;
for(int i=head[a];i!=- && r<b;i=edge[i].next){
int u=edge[i].e;
if(edge[i].v> && d[u]==d[a]+){
int x=find_min(edge[i].v,b-r);
x=dfs(u,x);
r+=x;
edge[i].v-=x;
edge[i^].v+=x;
}
}
if(!r)d[a]=-;
return r;
} int dinic(int sp,int tp){
int total=,t;
while(bfs()){
while(t=dfs(sp,MAX))
total+=t;
}
return total;
}
void init(int index){
int i,j;
e_num=;
memset(head,-,sizeof(head));
for(i=;i<=m;i++){
if(mat[i].v<=index)AddEdge(mat[i].s,mat[i].e,);
}
} int main()
{
int t_num,i,j,a,b,c;
while(~scanf("%d%d%d",&n,&m,&t_num)){
sp=;tp=n;
int high=-,low=MAX;
for(i=;i<=m;i++){
scanf("%d%d%d",&a,&b,&c);
high=find_max(high,c);
low=find_min(low,c);
mat[i].s=a; mat[i].e=b; mat[i].v=c;
}
int mid,ans;
while(low<=high){
mid=(low+high)/;
init(mid);
ans=dinic(sp,tp);
if(ans<t_num)low=mid+;
else high=mid-;
}
printf("%d\n",low);
}
return ;
}