【BZOJ 1877】 [SDOI2009]晨跑
来源:互联网 发布:补偿网络 编辑:程序博客网 时间:2024/05/17 06:05
带着 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;
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 }
0 0
- [BZOJ 1877][SDOI2009]晨跑
- 【BZOJ 1877】 [SDOI2009]晨跑
- bzoj 1877: [SDOI2009]晨跑
- 【BZOJ 1877】 [SDOI2009]晨跑
- bzoj 1877: [SDOI2009]晨跑
- BZOJ 1877: [SDOI2009]晨跑
- BZOJ 1877 [SDOI2009]晨跑
- bzoj 1877: [SDOI2009]晨跑
- bzoj 1877: [SDOI2009]晨跑
- bzoj 1877: [SDOI2009]晨跑 费用流
- Bzoj 1877: [SDOI2009]晨跑(费用流)
- [BZOJ 1877][SDOI2009]晨跑(费用流)
- BZOJ 1877: [SDOI2009]晨跑 费用流拆点
- BZOJ 1877 [SDOI2009]晨跑 费用流
- [SDOI2009]晨跑 bzoj 1877 费用流
- BZOJ P1877[SDOI2009]晨跑
- 1877: [SDOI2009]晨跑
- 1877: [SDOI2009]晨跑
- java数据结构 深搜+栈
- 【CodeVS 3123】 高精度练习之超大整数乘法
- 【BZOJ 1415】 [Noi2005]聪聪和可可
- 【BZOJ 2301】 单选错位
- Java String的构造和getByte
- 【BZOJ 1877】 [SDOI2009]晨跑
- KMP模板
- 【BZOJ 1355】 [Baltic2009]Radio Transmission
- 博客开更!!
- 1036: [ZJOI2008]树的统计Count
- UVA 11525 好大好大的排列(线段树)
- 数论基础
- eclipse配置android环境
- javah报错找不到类android.graphics.Bitmap的异常