题目大意:一个城市中有些点,有一些双向道路将这些点连接起来,每个点都有权值,求警察最少占据的点的权值和使得从A点无法到达B点。
思路:最小点割集签到题。
CODE:
#include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 80010 #define INF 0x3f3f3f3f using namespace std; #define min(a,b) ((a) < (b) ? (a):(b)) int points,edges; int S,T; int head[MAX],total = 1; int next[MAX],aim[MAX],flow[MAX]; inline void Add(int x,int y,int f) { next[++total] = head[x]; aim[total] = y; flow[total] = f; head[x] = total; } int deep[MAX]; inline bool BFS() { static queue<int> q; while(!q.empty()) q.pop(); memset(deep,0,sizeof(deep)); deep[S] = 1; q.push(S); while(!q.empty()) { int x = q.front(); q.pop(); for(int i = head[x]; i; i = next[i]) if(flow[i] && !deep[aim[i]]) { deep[aim[i]] = deep[x] + 1; q.push(aim[i]); if(aim[i] == T) return true; } } return false; } int Dinic(int x,int f) { if(x == T) return f; int temp = f; for(int i = head[x]; i; i = next[i]) if(flow[i] && temp && deep[aim[i]] == deep[x] + 1) { int away = Dinic(aim[i],min(flow[i],temp)); if(!away) deep[aim[i]] = 0; flow[i] -= away; flow[i^1] += away; temp -= away; } return f - temp; } int main() { cin >> points >> edges >> S >> T; S <<= 1,T = (T << 1|1); for(int x,i = 1; i <= points; ++i) { scanf("%d",&x); Add(i << 1,i << 1|1,x); Add(i << 1|1,i << 1,0); } for(int x,y,i = 1; i <= edges; ++i) { scanf("%d%d",&x,&y); Add(x << 1|1,y << 1,INF); Add(y << 1,x << 1|1,0); Add(y << 1|1,x << 1,INF); Add(x << 1,y << 1|1,0); } int max_flow = 0; while(BFS()) max_flow += Dinic(S,INF); cout << max_flow << endl; return 0; }
时间: 2024-11-08 14:57:25