【BZOJ 1877】 [SDOI2009]晨跑
2016-03-10 19:02
441 查看
带着 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
,nnext
,cost
,flow
,num
;
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
,fx
,d[500],team
,head,tail;
22 bool b
;
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 }
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
,nnext
,cost
,flow
,num
;
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
,fx
,d[500],team
,head,tail;
22 bool b
;
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 }
相关文章推荐
- 【BZOJ 2301】 单选错位
- 【BZOJ 1415】 [Noi2005]聪聪和可可
- iOS开发 提示框- UIAlertController(是UIAlertView和UIActionSheet的合二为一)
- cocos2d-x之物理引擎之碰撞监测
- CSS盒子模型
- 【洛谷 1338】 末日的传说
- PDO中文乱码的问题
- 【洛谷 1357】 花园
- 【BZOJ 1012】 [JSOI2008]最大数maxnumber
- 【洛谷 2161】 [Shoi2009]Booking 会场预约
- 【BZOJ 1798】[Ahoi2009]Seq 维护序列seq
- 【CodeVS 1540】银河英雄传说 2002年NOI全国竞赛
- 【洛谷 1991】 无线通讯网
- 【洛谷 1345】 奶牛的电信
- 【BZOJ 3990】 [SDOI2015]排序
- 【BZOJ 2705】 [SDOI2012]Longge的问题
- 【BZOJ 1934】 [Shoi2007]Vote 善意的投票
- 【BZOJ 1433】 [ZJOI2009]假期的宿舍
- 【COGS 1873】 [国家集训队2011]happiness(吴确)
- 【Poj 3469】 Dual Core CPU