Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
大牛们帮忙看看我为什么总是WA吧。。。谢谢!思路是:二分枚举一个边长度,凡是长度不大于枚举值的边设置容量为1,否则容量为0;求出最大流,判断是否大于t;如此反复。。。 代码如下: import java.io.*; import java.util.*; public class Main { static final int maxn=205; static final int maxm=40005; static final int maxint=0x7fffffff; static int side[]=new int[maxm]; static int cap[][]=new int[maxn][maxn]; static int flow[][]=new int[maxn][maxn]; static int graph[][]=new int[maxn][maxn]; static int pre[]=new int[maxn]; static int ls; static int n,p,t; static int max_flow; static int T,S; static void insert(int k) { int i; for(i=0;i<ls;i++) if(side[i]==k) break; if(i>=ls){ side[ls++]=k; } } static int min(int a,int b) { return a<b?a:b; } static int BFS() { int que[]=new int[maxn]; int mark[]=new int[maxn]; int i,u; int closed=0,open=-1; for(i=0;i<=n;i++) mark[i]=0; mark[1]=1; que[0]=1; while(open<closed) { open++; u=que[open]; for(i=1;i<=n;i++) if(cap[u][i]-flow[u][i]>0 && mark[i]==0){ closed++; que[closed]=i; mark[i]=1; pre[i]=u; if(i==T) return 1; } } return 0; } static void Edmonds_Karp() { int cp,v; while(BFS()==1) { cp=maxint; for(v=T;v!=S;v=pre[v]) cp=min(cp,cap[pre[v]][v]-flow[pre[v]][v]); for(v=T;v!=S;v=pre[v]){ flow[pre[v]][v]+=cp; flow[v][pre[v]]=-flow[pre[v]][v]; } max_flow+=cp; } } public static void main(String args[]) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String[] tmp; tmp=in.readLine().split(" "); n=Integer.parseInt(tmp[0]); p=Integer.parseInt(tmp[1]); t=Integer.parseInt(tmp[2]); int i,j,k,x,y,mid,ans=maxint; for(i=0;i<=n;i++) for(j=0;j<=n;j++) graph[i][j]=0; ls=0; for(i=0;i<p;i++){ tmp=in.readLine().split(" "); x=Integer.parseInt(tmp[0]); y=Integer.parseInt(tmp[1]); k=Integer.parseInt(tmp[2]); graph[x][y]=k; insert(k); } Arrays.sort(side,0,ls-1); x=0; y=ls-1; mid=(x+y)/2; S=1; T=n; while(x<=y){ mid=(x+y)/2; for(i=0;i<=n;i++) for(j=0;j<=n;j++) flow[i][j]=0; for(i=0;i<=n;i++) for(j=0;j<=n;j++) if(graph[i][j]<=side[mid] && graph[i][j]>0) cap[i][j]=1; else cap[i][j]=0; max_flow=0; Edmonds_Karp(); if(max_flow>=t){ y=mid-1; ans=min(ans,side[mid]); } else x=mid+1; } System.out.println(ans); } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator