POJ1459
来源:互联网 发布:小米平板2怎么连接网络 编辑:程序博客网 时间:2024/06/15 05:40
Problem : Power Network
Description : 有一个电网,一些基地可以生产电力,一些基地要消耗电力,而基地与基地之间有电线,这些电线有最大的电流量限制。问你这个电网最多消耗多少电力。
Solution : 最大流。在把超级源点向可以生产电力的基地连边,把超级汇点向消耗电力的基地两边。最后跑一边最大流就可以了。
Code(C++) :
#include <stdio.h>#include <string.h>#include <iostream>#include <queue>#define MIN(a,b) ((a)>(b)? (b):(a))using namespace std;const int SIZE=100+25;const int INF=0x3f3f3f3f;int src,des;int n,np,nc,m;int map[SIZE][SIZE];int d[SIZE];void build(){ memset(map,0,sizeof(map)); src=n; des=n+1; int from,to,value; char str[SIZE]; for(int i=0;i<m;i++) scanf("%s",str), sscanf(str,"(%d,%d)%d",&from,&to,&value), map[from][to]=value; for(int i=0;i<np;i++) scanf("%s",str), sscanf(str,"(%d)%d",&from,&value), map[src][from]=value; for(int i=0;i<nc;i++) scanf("%s",str), sscanf(str,"(%d)%d",&from,&value), map[from][des]=value;}bool bfs(int src,int des){ memset(d,-1,sizeof(d)); queue<int> que; que.push(src); d[src]=0; while(!que.empty()){ int now=que.front(); que.pop(); for(int i=0;i<=1+n;i++) if(d[i]==-1&&map[now][i]>0){ d[i]=d[now]+1; que.push(i); } } return d[des]>=0;}int dfs(int t,int sum,int des){ if(t==des) return sum; int tmp=sum; for(int i=0;i<=n+1&∑i++) if(d[i]==d[t]+1&&map[t][i]>0){ int a=dfs(i,MIN(sum,map[t][i]),des); map[t][i]-=a; map[i][t]+=a; sum-=a; } return tmp-sum;}int DINIC(int src,int des){ int sum=0; while(bfs(src,des)) sum+=dfs(src,INF,des); return sum;}int main(){ //freopen("in.data","r",stdin); while(~scanf("%d%d%d%d",&n,&np,&nc,&m)){ build(); printf("%d\n",DINIC(src,des)); } return 0;}
0 0
- poj1459
- POJ1459
- poj1459
- poj1459
- poj1459
- poj1459
- POJ1459
- poj1459
- POJ1459
- POJ1459
- poj1459
- poj1459 dinic
- 网络流(poj1459)
- poj1459 Power Network
- POJ1459解题报告
- poj1459(网络流)
- poj1459 网络流经典
- poj1459 Power Network
- CXF与Spring框架的整合
- Android Studio入门到精通
- 题目274 正三角形的外界圆面积
- Different sampler types for same sample texture unit in fragment shader.
- caffe命令行解析
- POJ1459
- Java-可重入锁
- Python中的MD5
- iOS沙盒访问方式
- 策略模式(strategy)
- ImageView定宽,高度等比例放大
- Linux线程同步-----条件变量
- oracle数据库删除操作
- dagger2简单使用