Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 6483 | Accepted: 2428 | |
Case Time Limit: 1000MS |
Description
Input
Output
Sample Input
2 3 2 0 3 2 1 1 3 0 3 2 0 2 3 0 1 0 1 2 1 0 2 1 0 0 2 0
Sample Output
2
Source
#include<cstdio> using namespace std; const int mm=55555; const int mn=333; const int oo=1000000000; int node,src,dest,edge; int ver[mm],flow[mm],next[mm]; int head[mn],work[mn],dis[mn],q[mn]; int d[mn][mn]; inline int min(int a,int b) { return a<b?a:b; } inline void prepare(int _node,int _src,int _dest) { node=_node,src=_src,dest=_dest; for(int i=0;i<node;++i)head[i]=-1; edge=0; } inline void addedge(int u,int v,int c) { ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++; ver[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++; } bool Dinic_bfs() { int i,u,v,l,r=0; for(i=0;i<node;++i)dis[i]=-1; dis[q[r++]=src]=0; for(l=0;l<r;++l) for(i=head[u=q[l]];i>=0;i=next[i]) if(flow[i]&&dis[v=ver[i]]<0) { dis[q[r++]=v]=dis[u]+1; if(v==dest)return 1; } return 0; } int Dinic_dfs(int u,int exp) { if(u==dest)return exp; for(int &i=work[u],v,tmp;i>=0;i=next[i]) if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0) { flow[i]-=tmp; flow[i^1]+=tmp; return tmp; } return 0; } int Dinic_flow() { int i,ret=0,delta; while(Dinic_bfs()) { for(i=0;i<node;++i)work[i]=head[i]; while(delta=Dinic_dfs(src,oo))ret+=delta; } return ret; } int main() { int u,v,w,n,c,m,s,l,r,mid; while(scanf("%d%d%d",&n,&c,&m)!=-1) { s=n+c; for(u=1;u<=s;++u) for(v=1;v<=s;++v) scanf("%d",&d[u][v]); for(w=1;w<=s;++w) for(u=1;u<=s;++u) if(w!=u)for(v=1;v<=s;++v) if(u!=v&&w!=v&&d[u][w]&&d[w][v]) { if(d[u][v])d[u][v]=min(d[u][v],d[u][w]+d[w][v]); else d[u][v]=d[u][w]+d[w][v]; } l=oo,r=0; for(u=1;u<=s;++u) for(v=1;v<=s;++v) if(d[u][v]) { if(d[u][v]<l)l=d[u][v]; if(d[u][v]>r)r=d[u][v]; } while(l<r) { mid=(l+r)>>1; prepare(s+2,0,s+1); for(u=1;u<=n;++u)addedge(src,u,m); for(u=n+1;u<=s;++u)addedge(u,dest,1); for(u=1;u<=n;++u) for(v=n+1;v<=s;++v) if(d[u][v]&&d[u][v]<=mid)addedge(u,v,1); if(Dinic_flow()==c)r=mid; else l=mid+1; } printf("%d\n",r); } return 0; }