题目链接:点击打开链接
题意:
题意:给出一张N(N<=100)个点,M(M<=1000条)边的有向图。每个点上都有一些人。每条边有4个属性(u,v,w,p)。这些边分为三种:(1)p<0时,表示这条边是隧道,这条隧道从u连向v,虽然如果想通过这条隧道的话没有流量限制,但可以最多只容纳w人;(2)p=0时,这条边是道路,由u连向v,通过没有流量限制;(3)p>0时,表示这条边是古老的桥,u连向v,如果不修这座桥,则只能通过1人,但是如果花费w的费用修桥的话,则通过这座桥的流量便没有限制。桥的总数<12。求使得最多的人能够躲到隧道里时候的人数和在该情况下的最小费用。
思路:
可以建立最大流模型来求解, 增加一个源点S,和一个汇点T。 S向每个有人的点,连一条容量为人数的边, 图中普通的u->v的有向边,连一条u->v的流量为无穷的边,
桥的流量则为1。 对于隧道,每个隧道可以虚拟出一个点,如u->v的隧道,可以虚拟一个点x,连接u->x,x->v的流量无穷的边, 和x->T的流量为隧道人数上限的边, 求解最大流即可得到最大人数。
状压一下桥,然后写写写
特别的是,对于能住人的隧道x: u,v,maxpeople, 是 u->x,inf; x->v,inf; x->to,maxpeople;
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<vector> template <class T> inline bool rd(T &ret) { char c; int sgn; if(c=getchar(),c==EOF) return 0; while(c!='-'&&(c<'0'||c>'9')) c=getchar(); sgn=(c=='-')?-1:1; ret=(c=='-')?0:(c-'0'); while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0'); ret*=sgn; return 1; } template <class T> inline void pt(T x) { if (x <0) { putchar('-'); x = -x; } if(x>9) pt(x/10); putchar(x%10+'0'); } using namespace std; #define ll int const int MAXN = 105;//点数的最大值 const int MAXM = 4000;//边数的最大值 const int INF = 0x3f3f3f3f; struct Edge { int to,next,cap,flow; }edge[MAXM];//注意是MAXM int tol; int head[MAXN]; int gap[MAXN],dep[MAXN],cur[MAXN]; void add(int u,int v,int w,int rw = 0) { edge[tol].to = v; edge[tol].cap = w; edge[tol].flow = 0; edge[tol].next = head[u]; head[u] = tol++; edge[tol].to = u; edge[tol].cap = rw; edge[tol].flow = 0; edge[tol].next = head[v]; head[v] = tol++; } int Q[MAXN]; void BFS(int start,int end) { memset(dep,-1,sizeof(dep)); memset(gap,0,sizeof(gap)); gap[0] = 1; int front = 0, rear = 0; dep[end] = 0; Q[rear++] = end; while(front != rear) { int u = Q[front++]; for(int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if(dep[v] != -1)continue; Q[rear++] = v; dep[v] = dep[u] + 1; gap[dep[v]]++; } } } int S[MAXN]; int sap(int start,int end,int N) { BFS(start,end); memcpy(cur,head,sizeof(head)); int top = 0; int u = start; int ans = 0; while(dep[start] < N) { if(u == end) { int Min = INF; int inser; for(int i = 0;i < top;i++) if(Min > edge[S[i]].cap - edge[S[i]].flow) { Min = edge[S[i]].cap - edge[S[i]].flow; inser = i; } for(int i = 0;i < top;i++) { edge[S[i]].flow += Min; edge[S[i]^1].flow -= Min; } ans += Min; top = inser; u = edge[S[top]^1].to; continue; } bool flag = false; int v; for(int i = cur[u]; i != -1; i = edge[i].next) { v = edge[i].to; if(edge[i].cap - edge[i].flow && dep[v]+1 == dep[u]) { flag = true; cur[u] = i; break; } } if(flag) { S[top++] = cur[u]; u = v; continue; } int Min = N; for(int i = head[u]; i != -1; i = edge[i].next) if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min) { Min = dep[edge[i].to]; cur[u] = i; } gap[dep[u]]--; if(!gap[dep[u]])return ans; dep[u] = Min + 1; gap[dep[u]]++; if(u != start)u = edge[S[--top]^1].to; } return ans; } void init(){ tol = 0; memset(head,-1,sizeof(head)); } struct node{ int u, v, w; node(int a=0,int b=0,int c=0):u(a),v(b),w(c){} }E[20],D[MAXM],C[MAXM]; int n, m, a[MAXN]; int siz, dtop, ctop, ans1, ans2; void build(int x){ init(); int from = 0, to = n+ctop+1; for(int i = 1; i <= n; i++) add(from, i, a[i]); for(int i = 0; i < dtop; i++) add(D[i].u, D[i].v, INF); for(int i = 0; i < ctop; i++){ add(C[i].u, n+1+i, INF); add(n+1+i, C[i].v, INF); add(n+1+i, to, C[i].w); } int cost = 0; for(int i = 0; i < siz; i++) if(x&(1<<i)) add(E[i].u, E[i].v, INF), cost += E[i].w; else add(E[i].u, E[i].v, 1); int flow = sap(from, to, to+1); if(flow > ans1) { ans1 = flow; ans2 = cost; } else if(flow == ans1) ans2 = min(ans2, cost); } void input(){ for(int i = 1; i <= n; i++)rd(a[i]); ctop = dtop = siz = 0; int u, v, w, type; while(m--){ rd(u);rd(v); rd(w); rd(type); if(type == 0) D[dtop++] = node(u,v,w); else if(type < 0) C[ctop++] = node(u,v,w); else if(type > 0) E[siz++] = node(u,v,w); } } int main(){ while(cin>>n>>m){ input(); ans1 = 0, ans2 = 0; for(int i = 0; i < (1<<siz); i++) build(i); if(ans1 == 0) puts("Poor Heaven Empire"); else cout<<ans1<<" "<<ans2<<endl; } return 0; }
时间: 2024-10-18 20:08:41