Description
N个点,每个点有初始的人数 ,三种通道
1、隧道:可以用来躲避,有固定的容量,也可以用来传递
2、道路:可以无限的通过
3、桥:不花费的话能通过一人,修之后可以无限通过
问最少花费最大可以隐藏人数。
Input
多组输入,每组输入第一行为两个整数N和M分别表示点数和通道数,第二行N个数表示每个点的初始人数,最后M行每行四个整数U,V,W,P,U和V表示这条通道的起点和终点,P表示这条通道的种类,如果P<0则这条通道为隧道,W此时表示这条隧道的容纳量,P=0则这条通道为普通道路,W无意义,P>0则这条通道为桥,W表示修复其的花费,以文件尾结束输入
Output
对于每组输入,输出使得最多人数隐藏在隧道中的最小花费,如果没有人可以隐藏在隧道中则输出Poor Heaven Empire
Sample Input
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
Sample Output
4 0
4 3
Solution
因为桥的数量不会超过12,所以可以最多枚举2^12种状态,在确定修复哪些桥的前提下建图求最小费用最大流,源点到每个点建容量为该点人数的边,对于隧道,其起点和终点间建容量为无穷的边,起点到汇点建容量为其容纳量的边,对于道路,其起点和终点间建容量为无穷的边,对于桥,若该状态修复了这座桥,则将其起点和终点间建容量为无穷的边,否则建容量为1的边,对于每种状态更新最大流和最小花费即可
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
#define maxn 1111
#define maxm 1111111
#define INF 0x3f3f3f3f
int head[maxn],cur[maxn],d[maxn],st[maxm],s,e,no,n;
struct point
{
int u,v,flow,next;
point(){};
point(int x,int y,int z,int w):u(x),v(y),next(z),flow(w){};
}p[maxm];
void add(int x,int y,int z)
{
p[no]=point(x,y,head[x],z);
head[x]=no++;
p[no]=point(y,x,head[y],0);
head[y]=no++;
}
void init()
{
memset(head,-1,sizeof(head));
no=0;
}
bool bfs()
{
int i,x,y;
queue<int>q;
memset(d,-1,sizeof(d));
d[s]=0;
q.push(s);
while(!q.empty())
{
x=q.front();
q.pop();
for(i=head[x];i!=-1;i=p[i].next)
{
if(p[i].flow&& d[y = p[i].v]<0)
{
d[y]=d[x]+1;
if(y==e)
return true;
q.push(y);
}
}
}
return false;
}
int dinic()
{
int i,loc,top,x=s,nowflow,maxflow=0;
while(bfs()){
for(i=s;i<=e;i++)
cur[i]=head[i];
top=0;
while(true)
{
if(x==e)
{
nowflow=INF;
for(i=0;i<top;i++)
{
if(nowflow>p[st[i]].flow)
{
nowflow=p[st[i]].flow;
loc=i;
}
}
for(i=0;i<top;i++)
{
p[st[i]].flow-=nowflow;
p[st[i]^1].flow+=nowflow;
}
maxflow+=nowflow;
top=loc;
x=p[st[top]].u;
}
for(i=cur[x];i!=-1;i=p[i].next)
if(p[i].flow&&d[p[i].v]==d[x]+1)
break;
cur[x]=i;
if(i!=-1)
{
st[top++]=i;
x=p[i].v;
}
else
{
if(!top)
break;
d[x]=-1;
x=p[st[--top]].u;
}
}
}
return maxflow;
}
int N,M,num[maxn],U[maxn],V[maxn],W[maxn],P[maxn];
int main()
{
while(~scanf("%d%d",&N,&M))
{
for(int i=1;i<=N;i++)
scanf("%d",&num[i]);
int k=0;//桥的数量
int flag=0;//标记是否有隧道
for(int i=1;i<=M;i++)
{
scanf("%d%d%d%d",&U[i],&V[i],&W[i],&P[i]);
if(P[i]>0)
k++;
if(P[i]<0)
flag=1;
}
if(!flag)//没有隧道
{
puts("Poor Heaven Empire");
continue;
}
int ans=0;//最大流即能够躲进隧道的最大人数
int cost=0;//最小费用
for(int i=0;i<(1<<k);i++)//枚举2^12个状态
{
init();//初始化
s=0;//源点为0
e=N+1;//汇点为N+1
int tcost=0;
int temp=0;
for(int j=1;j<=N;j++)//源点到每个点建容量为该点人数的边
add(s,j,num[j]);
for(int j=1;j<=M;j++)
{
if(P[j]<0)//隧道
{
add(U[j],V[j],INF);//隧道两端建容量为无穷的边
add(U[j],e,W[j]);//隧道起点到汇点建容量为隧道容纳量的边
}
if(P[j]==0)//道路
{
add(U[j],V[j],INF);//道路两端建容量为无穷的边
}
if(P[j]>0)//桥
{
if(i&(1<<temp))//该状态需要修复这座桥
{
add(U[j],V[j],INF);//桥两端建容量为无穷的边
tcost+=W[j];
}
else//不修复这座桥
add(U[j],V[j],1);//桥两端建容量为1的边
temp++;
}
}
int tans=dinic();
if(tans>ans||(tans==ans&&tcost<cost))//如果流量增加或者流量相同花费减少则更新答案
{
ans=tans;
cost=tcost;
}
}
if(ans==0)//没有人可以躲进隧道
puts("Poor Heaven Empire");
else//输出躲进隧道的最大人数和最小花费
printf("%d %d\n",ans,cost);
}
return 0;
}