共2*n+2个点
源点 到 i(1<=i<=n),流量为 每个城市的人
p<0, 表示u到v之间的边可存w人,这个边也可以通过人,
i+n(1<=i<=n)到汇点,流量为w
u到v,流量为 正无穷
p==0,u到v可以通过人
u到v,流量为 正无穷
p>0,为桥,可通过一人,修好后可通过 正无穷 人,边最多有12 个
u到v,流量为 1
2^12,枚举p>0的桥的所有可能情况,求最大流时的,最小费用
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define ll __int64
#define inf 0x3f3f3f3f
const int maxnode = 200 + 5;
const int maxedge = 10000 + 5;//开边数 x2.。
const int oo = 1000000000;
int node, src, dest, nedge;
int head[maxnode], point[maxedge], next1[maxedge], flow[maxedge], capa[maxedge];//point[x]==y表示第x条边连接y,head,next为邻接表,flow[x]表示x边的动态值,capa[x]表示x边的初始值
int dist[maxnode], Q[maxnode], work[maxnode];//dist[i]表示i点的等级
void init(int _node, int _src, int _dest){//初始化,node表示点的个数,src表示起点,dest表示终点
node = _node;
src = _src;
dest = _dest;
for (int i = 0; i < node; i++) head[i] = -1;
nedge = 0;
}
void addedge(int u, int v, int c1, int c2){//增加一条u到v流量为c1,v到u流量为c2的两条边
point[nedge] = v, capa[nedge] = c1, flow[nedge] = 0, next1[nedge] = head[u], head[u] = (nedge++);
point[nedge] = u, capa[nedge] = c2, flow[nedge] = 0, next1[nedge] = head[v], head[v] = (nedge++);
}
bool dinic_bfs(){
memset(dist, 255, sizeof (dist));
dist[src] = 0;
int sizeQ = 0;
Q[sizeQ++] = src;
for (int cl = 0; cl < sizeQ; cl++)
for (int k = Q[cl], i = head[k]; i >= 0; i = next1[i])
if (flow[i] < capa[i] && dist[point[i]] < 0){
dist[point[i]] = dist[k] + 1;
Q[sizeQ++] = point[i];
}
return dist[dest] >= 0;
}
int dinic_dfs(int x, int exp){
if (x == dest) return exp;
for (int &i = work[x]; i >= 0; i = next1[i]){
int v = point[i], tmp;
if (flow[i] < capa[i] && dist[v] == dist[x] + 1 && (tmp = dinic_dfs(v, min(exp, capa[i] - flow[i]))) > 0){
flow[i] += tmp;
flow[i^1] -= tmp;
return tmp;
}
}
return 0;
}
int dinic_flow(){
int result = 0;
while (dinic_bfs()){
for (int i = 0; i < node; i++) work[i] = head[i];
while (1){
int delta = dinic_dfs(src, oo);
if (delta == 0) break;
result += delta;
}
}
return result;
}
//建图前,运行一遍init();
//加边时,运行addedge(a,b,c,0),表示点a到b流量为c的边建成(注意点序号要从0开始)
//求解最大流运行dinic_flow(),返回值即为答案
int p[110];
int p3[13][3];
int p1[1010][3];
int p2[1010][2];
int main()
{
int n,m,i,j,k,a,b,c,d;
while(cin>>n>>m)
{
for(i=1;i<=n;i++)
scanf("%d",&p[i]);
int t1=0,t2=0,t3=0;
for(i=1;i<=m;i++)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
if(d<0) p1[t1][0]=a,p1[t1][1]=b,p1[t1][2]=c,t1++;
else if(d==0) p2[t2][0]=a,p2[t2][1]=b,t2++;
else p3[t3][0]=a,p3[t3][1]=b,p3[t3][2]=c,t3++;
}
int end=(1<<t3);
int maxflow=0,cost=inf;
for(i=0;i<end;i++)
{
int sum=0;
init(2*n+2,0,2*n+1);
for(j=1;j<=n;j++){
addedge(0,j,p[j],0);
addedge(n+j,2*n+1,inf,0);
}
for(j=0;j<t2;j++)
addedge(p2[j][0],p2[j][1],inf,0);
for(j=0;j<t1;j++){
addedge(p1[j][0],p1[j][1]+n,p1[j][2],0);
addedge(p1[j][0],p1[j][1],inf,0);
}
for(j=0;j<t3;j++)
{
addedge(p3[j][0],p3[j][1],1,0);
if((i>>j)&1){
addedge(p3[j][0],p3[j][1],inf,0);
sum+=p3[j][2];
}
}
int ans=dinic_flow();
if(ans>maxflow){
maxflow=ans;
cost=sum;
}
else if(maxflow==ans && sum<cost)
cost=sum;
}
if(maxflow==0)
printf("Poor Heaven Empire\n");
else
printf("%d %d\n",maxflow,cost);
}
}