带着 freopen提交会TLE.......
1 #include <cstdio>
2 #include <iostream>
3 #include <string>
4 #include <algorithm>
5 using namespace std;
6 #define N 1000000
7 #define INF 1000000000
8 int n,m,s,t;
9 int tot=1,g[N],nnext[N],cost[N],flow[N],num[N];
10 void Add(int x,int y,int z,int f)
11 {
12 // cout<<x<<‘ ‘<<y<<‘ ‘<<z<<‘ ‘<<f<<endl;
13 tot++;
14 nnext[tot]=g[x];
15 g[x]=tot;
16 num[tot]=y;
17 cost[tot]=z;
18 flow[tot]=f;
19 }
20
21 int fa[N],fx[N],d[500],team[N],head,tail;
22 bool b[N];
23 bool SPFA()
24 {
25 // cout<<endl;
26 for(int i=1;i<=n*2;i++) d[i]=INF;
27 d[s]=0;
28 head=tail=0;
29 team[++tail]=s; b[s]=true;
30 while(head<tail)
31 {
32 int x=team[++head];b[x]=false; //cout<<x<<‘ ‘;
33 for(int i=g[x];i;i=nnext[i])
34 if(d[num[i]]>d[x]+cost[i]&&flow[i]!=0)
35 {
36 d[num[i]]=d[x]+cost[i]; //cout<<num[i]<<‘ ‘;
37 fa[num[i]]=x;
38 fx[num[i]]=i;
39 if(!b[num[i]])
40 {
41 b[num[i]]=true;
42 team[++tail]=num[i];
43 }
44 }//cout<<endl;
45 }
46 if(d[t]==INF) return false;
47 return true;
48 }
49 int main()
50 {
51 // freopen("a.in","r",stdin);
52
53 scanf("%d %d",&n,&m);s=1+n,t=n;
54 for(int i=1;i<=m;i++)
55 {
56 int x,y,z;
57 scanf("%d %d %d",&x,&y,&z);
58 Add(x+n,y,z,1);
59 Add(y,x+n,-z,0);
60 }
61 for(int i=1;i<=n;i++)
62 {
63 Add(i,i+n,0,1);
64 Add(i+n,i,0,0);
65 }
66 int max_flow=0,min_cost=0;
67 while(SPFA())
68 {
69 max_flow++;
70 for(int i=t;i!=s;i=fa[i])
71 {
72 // cout<<i<<‘ ‘<<cost[fx[i]]<<endl;
73 int tmp=fx[i];
74 min_cost+=cost[tmp];
75 flow[tmp]--;
76 flow[tmp^1]++;
77 }//cout<<min_cost<<endl;cout<<endl;
78 }
79 printf("%d %d\n",max_flow,min_cost);
80 return 0;
81 }