您的位置:首页 > 理论基础 > 计算机网络

hdu 4309 Seikimatsu Occult Tonneru(网络流,4级)

2013-05-30 15:33 459 查看

Seikimatsu Occult Tonneru

Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1357 Accepted Submission(s): 317



[align=left]Problem Description[/align]
During the world war, to avoid the upcoming Carpet-bombing from The Third Reich, people in Heaven Empire went to Great Tunnels for sheltering.

There are N cities in Heaven Empire, where people live, with 3 kinds of directed edges connected with each other. The 1st kind of edges is one of Great Tunnels( no more than 20 tunnels) where a certain number of people can hide here; people can also go through
one tunnel from one city to another. The 2nd kind of edges is the so-called Modern Road, which can only let people go through. The 3rd kind of edges is called Ancient Bridge and all the edges of this kind have different names from others, each of which is
named with one of the twelve constellations( such as Libra, Leo and so on); as they were build so long time ago, they can be easily damaged by one person's pass. Well, for each bridge, you can spend a certain deal of money to fix it. Once repaired, the 3rd
kind of edges can let people pass without any limitation, namely, you can use one bridge to transport countless people. As for the former two kinds of edges, people can initially go through them without any limitation.

We want to shelter the most people with the least money.

Now please tell me the largest number of people who can hide in the Tunnels and the least money we need to spend to realize our objective.

[align=left]Input[/align]
Multiple Cases.

The first line, two integers: N (N<=100), m (m<=1000). They stands for the number of cities and edges.

The next line, N integers, which represent the number of people in the N cities.

Then m lines, four intergers each: u, v, w, p (1<=u, v<=N, 0<=w<=50). A directed edge u to v, with p indicating the type of the edge: if it is a Tunnel then p < 0 and w means the maximum number people who can hide in the the tunnel; if p == 0 then it is a Modern
Road with w means nothing; otherwise it is an Ancient Bridge with w representing the cost of fixing the bridge. We promise there are no more than one edge from u to v.

[align=left]Output[/align]
If nobody can hide in the Tunnels, print “Poor Heaven Empire”, else print two integers: maximum number and minimum cost.

[align=left]Sample Input[/align]

4 4
2 1 1 0
1 2 0 0
1 3 0 0
2 4 1 -1
3 4 3 -1

4 4
2 1 1 0
1 2 0 0
1 3 3 1
2 4 1 -1
3 4 3 -1


[align=left]Sample Output[/align]

4 0
4 3


[align=left]Author[/align]
BUPT

[align=left]Source[/align]
2012 Multi-University Training Contest 1

[align=left]Recommend[/align]
zhuyuanchen520

思路:网络流,源点到有人城市建边权为人口数,正常路建边权无穷,能藏人的路建无穷边,再建到汇点权为能藏的人数(最好拆点),最后枚举桥,修则权无穷否则为1

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int mm=5e3+9;
const int mn=2e3+9;
const int oo=1e9;
class knode
{
public:
int next,flow,v;
} e[mm];
class znode
{
public:
int u,v,w;
} f[3][mm];
int pos[3];
int q[mn],head[mn],work[mn],dis[mn],peo[mn];
int src,dest,node,edge,n,m;
void data(int _node,int _src,int _dest)
{
node=_node;
src=_src;
dest=_dest;
edge=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int c)
{
e[edge].v=v;
e[edge].flow=c;
e[edge].next=head[u];
head[u]=edge++;
e[edge].v=u;
e[edge].flow=0;
e[edge].next=head[v];
head[v]=edge++;
}
bool bfs()
{
int l=0,r=1,u,v;
for(int i=0; i<node; ++i)dis[i]=-1;
q[l]=src;
dis[src]=0;
while(l^r)
{
u=q[l++];
l%=mn;
for(int i=head[u]; ~i; i=e[i].next)
{
v=e[i].v;
if(e[i].flow&&dis[v]==-1)
{
dis[v]=dis[u]+1;
q[r++]=v;
r%=mn;
if(v==dest)
{
return 1;
}
}
}
}
return 0;
}
int dfs(int u,int exp)
{
if(u==dest)return exp;
int tmp=0,v;
for(int&i=work[u]; ~i; i=e[i].next)
{
v=e[i].v;
if(e[i].flow&&dis[v]==dis[u]+1&&(tmp=dfs(v,min(e[i].flow,exp)))>0)
{
e[i].flow-=tmp;
e[i^1].flow+=tmp;
return tmp;
}
}
return 0;
}

int dinic_flow()
{
int ret=0,num=0;
while(bfs())
{
for(int i=0; i<node; ++i)work[i]=head[i];
while(num=dfs(src,oo))ret+=num;
}
return ret;
}
int main()
{
int a,b,c,d,n,m;
while(~scanf("%d%d",&n,&m))
{
pos[0]=pos[1]=pos[2]=0;
for(int i=1; i<=n; ++i)
scanf("%d",&peo[i]);
for(int i=0; i<m; ++i)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
if(d<0)f[2][pos[2]].u=a,f[2][pos[2]].v=b,f[2][pos[2]++].w=c;
else if(d==0)f[1][pos[1]].u=a,f[1][pos[1]].v=b,f[1][pos[1]++].w=c;
else f[0][pos[0]].u=a,f[0][pos[0]].v=b,f[0][pos[0]++].w=c;
}
int z=1<<pos[0];
int cost=oo,cc,pe,people=0;
for(int i=0; i<z; ++i)
{
cc=0;
data(n+2+pos[2],0,n+1+pos[2]);
for(int j=0; j<pos[0]; ++j)

if(i&(1<<j))
{
cc+=f[0][j].w;
add(f[0][j].u,f[0][j].v,oo);
}
else
{
add(f[0][j].u,f[0][j].v,1);
}
for(int j=1; j<=n; ++j)
if(peo[j])
{
add(src,j,peo[j]);
}
for(int j=0; j<pos[1]; ++j)
add(f[1][j].u,f[1][j].v,oo);
for(int j=0; j<pos[2]; ++j)
{
add(f[2][j].u,n+j+1,oo),add(n+j+1,f[2][j].v,oo),add(n+j+1,dest,f[2][j].w);
}
pe=dinic_flow();
if(pe>people)
{
people=pe;
cost=cc;
}
else if(pe==people&&cc<cost)cost=cc;
}
if(people)printf("%d %d\n",people,cost);
else puts("Poor Heaven Empire");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: