最近为了准备免修考试而在学习java和python,然后就拿了一道网络流来练练手,不过虽然是java跟python不过其实还是C++直接翻译过来的啦,所以哪些很棒的特性我一点都没有用~~ 题目:D:Til the Cows Come Home

 1 import java.util.*;
 2 import java.lang.*;
 3 class edge{
 4     int to,nxt,cap;
 5     edge(int x,int y,int z){to = x;nxt = y;cap = z; }
 6 }
 7 public class Main{
 8     static int n,m;
 9     static edge[] edges = new edge[410];
10     static int[] nxt = new int[210],p;
11     static int[] gap = new int[210],h = new int[210];
12     static int l;
13     static void addedge(int x,int y,int z) {
14         l++;
15         edges[l*2]=new edge(y,nxt[x],z);nxt[x]=l*2;
16         edges[l*2+1]=new edge(x,nxt[y],0);nxt[y]=l*2+1;
17     }
18     static int sap(int u,int flow) {
19         if (u==n) return flow;
20         int cnt = 0;
21         for (int i=p[u];i!=0;i=edges[i].nxt){
22             if (edges[i].cap!=0&&h[u]==h[edges[i].to]+1) {
23                 int cur = sap(edges[i].to,Math.min(flow-cnt,edges[i].cap));
24                 edges[i].cap-=cur;edges[i^1].cap+=cur;
25                 p[u]=i;
26                 if ((cnt+=cur)==flow) return flow;
27             }
28         }
29         if ((--gap[h[u]])==0) h[1]=n;
30         gap[++h[u]]++;
31         p[u]=nxt[u];
32         return cnt;
33     }
34     static int maxflow(){
35         Arrays.fill(gap,0);
36         Arrays.fill(h,0);
37         p = Arrays.copyOf(nxt,210);
38         gap[0]=n;
39         int flow = 0;
40         while (h[1]<n) {
41             flow += sap(1,0x7fffffff);
42         }
43         return flow;
44     }
45     public static void main(String[] Args){
46         Scanner sc = new Scanner(System.in);
47         for (;;) {
48             try{
49                 m = sc.nextInt();n=sc.nextInt();
50                 l=0;
51                 nxt=new int[210];
52                 for (int i=0;i<m;++i) {
53                     int x=sc.nextInt(),y=sc.nextInt(),z=sc.nextInt();
54                     addedge(x,y,z);
55                 }
56                 System.out.println(maxflow());
57             }catch(Exception E){
58                 break;
59             }
60         }
61     }
62 }
View Code

相关文章:

  • 2021-09-16
  • 2021-11-17
  • 2021-05-28
  • 2021-10-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-29
  • 2021-11-15
猜你喜欢
  • 2021-06-13
  • 2022-12-23
  • 2021-08-04
  • 2022-12-23
  • 2021-07-31
  • 2021-06-02
相关资源
相似解决方案