poj1459(多源点网络流)
题意:有n个点,np个供电点,nc个消费点,m条线路,接来题目先给出的是m条带权路径,然后是np个供电点和权值,接着就是nc个消费点和权值。
题目要我们求出给定的图最大能消费的总电量(就是求最大流)
思路:建一个超级源点,一个超级汇点。超级源点连接所以供电站,所以消费点连接超级汇点。跑一遍网络流即可。
代码:
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<queue> #define inf 0x3f3f3f3f using namespace std; typedef long long ll; struct node{ int c,f; }mp[310][310]; int sx,ex,n,m,np,nc; int pre[310]; bool bfs() { memset(pre,0,sizeof(pre)); pre[sx]=1; queue<int> q; q.push(sx); while(!q.empty()) { int p=q.front(); q.pop(); for(int i=0;i<=n+1;i++) { if(!pre[i]&&mp[p][i].c-mp[p][i].f) { pre[i]=pre[p]+1; q.push(i); } } } return pre[ex]!=0; } int dinic(int pos,int flow) { int f=flow; if(pos==ex) return flow; for(int i=0;i<=n+1;i++) { if(mp[pos][i].c-mp[pos][i].f&&pre[pos]+1==pre[i]) { int a=mp[pos][i].c-mp[pos][i].f; int t=dinic(i,min(a,flow)); mp[pos][i].f+=t; mp[i][pos].f-=t; flow-=t; } } return f-flow; } int solve() { int sum=0; while(bfs()) { sum+=dinic(sx,inf); } return sum; } int main() { while(cin>>n>>np>>nc>>m) { int st,ed,v; char f; sx=n;ex=n+1; memset(mp,0,sizeof(mp)); for(int i=1;i<=m;i++) { cin>>f>>st>>f>>ed>>f>>v; if(st==ed) continue; mp[st][ed].c+=v; } for(int i=1;i<=np;i++) { cin>>f>>st>>f>>v; mp[sx][st].c+=v; } for(int i=1;i<=nc;i++) { cin>>f>>ed>>f>>v; mp[ed][ex].c+=v; } cout<<solve()<<endl; } return 0; }
转载于:https://www.cnblogs.com/xiongtao/p/11137771.html
- POJ 1459 PowerNetwork 多源点网络流入门(EK算法求最大流)
- poj 1459 多源点网络流入门题
- POJ1459 Power Network(多源点网络流,最大流,EK算法)
- poj 1459 Power Network(网络流最大流,多源点多汇点)
- poj1459网络流之多源点最大流
- Power Network - POJ 1459 - 网络流
- poj 1459 Power Network(网络流+dinic)
- poj 1459 Power Network 网络流
- POJ 1459 Power Network 网络流
- POJ 1459 ZOJ 1734 Power Network dinic 算法 网络流
- POJ 1459 网络流算法(EK)
- poj 1459 Power Network(网络流_最大流)
- POJ-1459 Power Network 网络流
- POJ 1459 Power Network (网络流最大流基础 多源点多汇点 Edmonds_Karp算法)
- poj1459 网络流经典
- POJ 1459-Power Network(网络流-最大流-ISAP)C++
- poj 1459 网络流isap和EK的比较
- 【网络流EK算法&压入重标记算法】POJ 1459
- [POJ 1459 Power Network] Dinic网络流
- poj1459 Power Network网络流最大流基础题