标签:sample numbers iss positive path nes build cep single
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 18083 | Accepted: 6460 | |
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
#include<queue> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=280; int head[N],dis[N],tot,K,C,M,st,ed,mp[N][N]; struct node { int to,next,w; } e[N*N*2]; void add(int u,int v,int w) { e[tot].to=v; e[tot].next=head[u]; e[tot].w=w; head[u]=tot++; } void init() { tot=0; memset(head,-1,sizeof(head)); } bool bfs() { queue<int>Q; memset(dis,-1,sizeof(dis)); Q.push(0); dis[0]=1; while(!Q.empty()) { int u=Q.front(); Q.pop(); for(int i=head[u]; i+1; i=e[i].next) { int v=e[i].to; if(dis[v]==-1&&e[i].w>0) { dis[v]=dis[u]+1; Q.push(v); if(v==K+C+1) return true; } } } return false; } int dfs(int s,int low) { if(s==ed||low==0) return low; int ans=low,a; for(int i=head[s]; i+1; i=e[i].next) { int v=e[i].to; if(e[i].w>0&&dis[v]==dis[s]+1&&(a=dfs(v,min(ans,e[i].w)))) { ans-=a; e[i].w-=a; e[i^1].w+=a; if(!ans) return low; } } if(ans==low) dis[s]=-1; return low-ans; } void build(int lim) { init(); for(int i=1; i<=K; ++i) for(int j=K+1; j<ed; ++j) if(mp[i][j]<=lim&&mp[i][j]) { add(i,j,1); add(j,i,0); } for(int i=1; i<=K; ++i) add(0,i,M),add(i,0,0); for(int i=K+1; i<ed; ++i) add(i,ed,1),add(ed,i,0); } bool Ju() { int ans=0; while(bfs()) ans+=dfs(0,99999999); if(ans==C) return true; return false; } void Floyd() { for(int k=1; k<ed; ++k) for(int i=1; i<ed; ++i) if(mp[i][k]) for(int j=1; j<ed; ++j) if(mp[k][j]) { if(mp[i][j]==0) mp[i][j]=mp[i][k]+mp[k][j]; else if(mp[i][j]>mp[i][k]+mp[k][j]) mp[i][j]=mp[i][k]+mp[k][j]; } } int main() { while(scanf("%d%d%d",&K,&C,&M)!=EOF) { st=0; ed=K+C+1; for(int i=1; i<ed; ++i) for(int j=1; j<ed; ++j) scanf("%d",&mp[i][j]); Floyd(); int r=20000000,l=0; while(r!=l) { int mid=(r+l)>>1; build(mid); if(Ju()) r=mid; else l=mid+1; } printf("%d\n",(l+r)>>1); } }
标签:sample numbers iss positive path nes build cep single
原文地址:http://www.cnblogs.com/mfys/p/7324014.html