pku 1459 Power Network EK&&Dinic算法解决。

http://poj.org/problem?id=1459

才开始套自己的那个模板就是过不了一直TLE,最后看了别人过得修改了一下自己的模板最后1391s险过。。。

本题是一个多源点,多汇点问题,建立超级源点与汇点,然后套用模板求解。。

EK O(V*E^2)

View Code
#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
const int inf=100000000;
const int maxn=105;
int map[maxn][maxn],flow[maxn];
bool vt[maxn];
int n,np,nc,m;
int pre[maxn];

int bfs(int s,int e)
{
int i;
for (i = 0; i <= n + 1; ++i)
flow[i] = inf;
memset(pre,-1,sizeof(pre));
queue<int>q;
q.push(s);
while (!q.empty())
{
int p = q.front(); q.pop();
for (i = 0; i <= n + 1; ++i)
{
if (i == s || map[p][i] <= 0 || pre[i] != -1)
continue;
vt[i] = true;
pre[i] = p;
flow[i] = min(flow[p],map[p][i]);//就是这里的优化,这里直接就找出了最小流量
//省去了我在EK中再循环一遍了。。。
q.push(i);
}
}
if (flow[e] == inf) return -1;
return flow[e];
}
int EK(int s,int e)
{
int i,d;
int ans = 0;
while (1)
{
d = bfs(s,e);
if (d == -1) break;
for (i = e; i != s; i = pre[i])
{
map[pre[i]][i] -= d;
map[i][pre[i]] += d;
}
ans += d;
}
return ans;
}
int main()
{
int i,x,y,v;
while (~scanf("%d%d%d%d",&n,&np,&nc,&m))
{
memset(map,0,sizeof(map));
for (i = 0; i < m; ++i)
{
scanf(" (%d,%d)%d",&x,&y,&v);
map[x][y] = v;
}
for (i = 0; i < np; ++i)
{
scanf(" (%d)%d",&x,&v);
map[n][x] = v;
}
for (i = 0; i < nc; ++i)
{
scanf(" (%d)%d",&x,&v);
map[x][n + 1] = v;
}
printf("%d\n",EK(n,n + 1));
}
return 0;
}

Dinic算法79过。。好快。。。O(V^2*E)

View Code
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#define maxn 107
#define inf 99999999
using namespace std;
int g[maxn][maxn],level[maxn];
bool vt[maxn];
//分层
bool layer(int s,int e)
{
queue<int>q;
memset(level,-1,sizeof(level));
q.push(s);
level[s] = 1;
int i,p;
while (!q.empty())
{
p = q.front();
q.pop();
for (i = 0; i <= e; ++i)
{
if (g[p][i] > 0 && level[i] == -1)//满足还没有分层,且p与i有流量
{
level[i] = level[p] + 1;
if (i == e) { return true;}
else q.push(i);
}
}
}
return false;
}
int dinic(int s,int e)
{
deque<int>q;//利用双端队列
int i,p,vs,ve,min,pos,sum;
sum = 0;
while (layer(s,e))
{
memset(vt,false,sizeof(vt));
q.push_back(s);//从后端插入
vt[s] = true;
while (!q.empty())
{
p = q.back();//从后端取值
if (p == e)//如果得到了终点
{
min = inf;
for (i = 1; i < q.size(); ++i)
{
vs = q[i - 1]; ve = q[i];
if (g[vs][ve] > 0 && g[vs][ve] < min)
{
min = g[vs][ve];
pos = vs;
}
}
sum += min;
for (i = 1; i < q.size(); ++i)
{
vs = q[i - 1]; ve = q[i];
if (g[vs][ve] > 0)
{
g[vs][ve] -= min;
g[ve][vs] += min;
}
}
while (!q.empty() && q.back() != pos)//返回最小流量的边的定点,重新找增广路
{
vt[q.back()] = false;
q.pop_back();
}
}
else//继续找下一层
{
for (i = 0; i <= e; ++i)
{
if (g[p][i] > 0 && !vt[i] && level[i] == level[p] + 1)
{
vt[i] = true;
q.push_back(i);
break;
}
}
if (i > e) q.pop_back();
}
}
}
return sum;
}
int main()
{
int i,n,m,np,nc;
int x,y,v;
while (~scanf("%d%d%d%d",&n,&np,&nc,&m))
{
memset(g,0,sizeof(g));
for (i = 0; i < m; ++i)
{
scanf(" (%d,%d)%d",&x,&y,&v);
g[x][y] = v;
}
for (i = 0; i < np; ++i)
{
scanf(" (%d)%d",&x,&v);
g[n][x] = v;
}
for (i = 0; i < nc; ++i)
{
scanf(" (%d)%d",&x,&v);
g[x][n + 1] = v;
}
printf("%d\n",dinic(n,n+1));
}
return 0;
}




posted @ 2012-03-17 11:21  E_star  阅读(361)  评论(0编辑  收藏  举报