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
4 0 4 3
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <string> #include <algorithm> #include <queue> using namespace std; const int maxn = 100+10; const int maxm = 1000+10; const int inf = 1<<25; int n,m,nume; struct edge{ int v,f,nxt; }; struct build{ int u,v,w; build(int u,int v,int w):u(u),v(v),w(w){} }; vector<build> vb[3]; edge e[maxm]; int head[maxn]; int city[maxn]; void addedge(int u,int v,int c){ e[++nume].nxt = head[u]; e[nume].v = v; e[nume].f = c; head[u] = nume; e[++nume].nxt = head[v]; e[nume].v = u; e[nume].f = 0; head[v] = nume; } void init(){ memset(head,0,sizeof head); nume = 1; } queue<int> que; bool vis[maxn]; int dist[maxn]; int src,sink; void bfs(){ memset(dist,0,sizeof dist); while(!que.empty()) que.pop(); vis[src] = true; que.push(src); while(!que.empty()){ int u = que.front(); que.pop(); for(int i = head[u]; i ; i = e[i].nxt){ if(e[i].f && !vis[e[i].v]){ que.push(e[i].v); vis[e[i].v] = 1; dist[e[i].v] = dist[u]+1; } } } } int dfs(int u,int delta){ if(u== sink) return delta; else{ int ret = 0; for(int i = head[u]; delta&&i; i = e[i].nxt){ if(e[i].f && dist[e[i].v] == dist[u]+1){ int dd = dfs(e[i].v,min(e[i].f,delta)); e[i].f -= dd; e[i^1].f += dd; delta -= dd; ret += dd; } } return ret; } } int maxflow(){ int ret = 0; while(true){ memset(vis,0,sizeof vis); bfs(); if(!vis[sink]) return ret; ret += dfs(src,inf); } } int main(){ while(~scanf("%d%d",&n,&m)){ src = 0; sink = n+1; for(int i = 0; i < 3; i++) vb[i].clear(); for(int i = 1; i <= n; i++) scanf("%d",&city[i]); bool flag = 0; while(m--){ int a,b,c,d; scanf("%d%d%d%d",&a,&b,&c,&d); if(d==0){ vb[1].push_back(build(a,b,c));//现代桥 } else if(d<0){ vb[0].push_back(build(a,b,c));//隧道 flag = 1; }else{ vb[2].push_back(build(a,b,c));//古代桥 } } if(!flag){ printf("Poor Heaven Empire\n"); continue; } int d = vb[2].size(); int maxf = -1,minc = inf; for(int i = 0; i < (1<<d); i++){ init(); int tc = 0; for(int j = 0; j < d; j++){ if(i&(1<<j)){ addedge(vb[2][j].u,vb[2][j].v,inf); tc += vb[2][j].w; }else{ addedge(vb[2][j].u,vb[2][j].v,1); } } for(int i = 1; i <= n; i++){ addedge(0,i,city[i]); } for(int i = 0; i < vb[1].size(); i++){ addedge(vb[1][i].u,vb[1][i].v,inf); } for(int i = 0; i < vb[0].size(); i++){ addedge(vb[0][i].u,vb[0][i].v,inf); addedge(vb[0][i].u,n+1,vb[0][i].w); } int tm = maxflow(); if(tm > maxf){ maxf = tm; minc = tc; } if(tm==maxf && minc > tc){ minc = tc; } } if(maxf==-1){ printf("Poor Heaven Empire\n"); }else{ printf("%d %d\n",maxf,minc); } } return 0; }
HDU4309-Seikimatsu Occult Tonneru(最大流),布布扣,bubuko.com
HDU4309-Seikimatsu Occult Tonneru(最大流)
原文:http://blog.csdn.net/mowayao/article/details/38384917