SGU 326 Perspective(网络流)

岛娘网络流专题里面的一道题。

又是个竞赛排名问题,跟上一篇WOJ的题目其实是一样的解法。只不过这道题要求第1支队伍是冠军,如果有队伍和第一支队伍同分的话也是可以的,这是和上一道题不同的地方。

代码如下:

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<utility>
#include<stack>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<set>
#include<map>
using namespace std;
const int maxn = 1000;
const int maxm = 1e5;
const int INF = 0x3f3f3f3f;
int n;

int head[maxn],cur[maxn],nx[maxm<<1],to[maxm<<1],flow[maxm<<1],ppp=0;
struct Dinic
{
	int dis[maxn];
	int s, t;
	long long ans;
	
	void init() {
		memset(head, -1, sizeof(head));
		ppp = 0;
	}
	
	void AddEdge(int u, int v, int c) {
		to[ppp]=v;flow[ppp]=c;nx[ppp]=head[u];head[u]=ppp++;swap(u,v);
		to[ppp]=v;flow[ppp]=0;nx[ppp]=head[u];head[u]=ppp++;
	}
	
	bool BFS() {
		memset(dis, -1, sizeof(dis));
		dis[s] = 1; 
		queue<int> Q;
		Q.push(s);
		while(!Q.empty()) {
			int x = Q.front();
			Q.pop();
			for(int i = head[x]; ~i; i = nx[i]) {
				if(flow[i] && dis[to[i]] == -1) {
					dis[to[i]] = dis[x] + 1;
					Q.push(to[i]);
				}
			}
		}
		return dis[t] != -1;
	}
	
	int DFS(int x, int maxflow) {
		if(x == t || !maxflow){
			ans += maxflow;
			return maxflow;
		}
		int ret = 0, f;
		for(int &i = cur[x]; ~i; i = nx[i]) {
			if(dis[to[i]] == dis[x] + 1 && (f = DFS(to[i], min(maxflow, flow[i])))) {
				ret += f;
				flow[i] -= f;
				flow[i^1] += f;
				maxflow -= f;
				if(!maxflow)
					break;
			}
		}
		return ret;
	}
	
	long long solve(int source, int tank) {
		s = source;
		t = tank;
		ans = 0;
		while(BFS()) {
			memcpy(cur, head, sizeof(cur));
			DFS(s, INF);
		}
		return ans;
	}
}dinic;

int score[maxn], r[maxn];
int G[maxn][maxn];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
#endif
	int sum = 0;
	long long cnt = 0;
	scanf("%d", &n);
	for(int i = 1; i <= n; i++)
		scanf("%d", &score[i]);
	for(int i = 1; i <= n; i++) {
		scanf("%d", &r[i]);
	}
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= n; j++) {
			scanf("%d", &G[i][j]);
			sum++;
			cnt += G[i][j];
		}
	}
	sum /= 2;
	cnt /= 2;
	for(int i = 2; i <= n; i++) {
		if(G[1][i] > 0) {
			score[1] += G[1][i];
			r[1] -= G[1][i];
			r[i] -= G[1][i];
			sum--;
			cnt -= G[1][i];
			G[1][i] = G[i][1] = 0;
		}
	}
	if(r[1] > 0)
		score[1] += r[1];
	bool flag = 0;
	for(int i = 2; i <= n; i++) {
		if(score[1] < score[i])
			flag = 1;
	}
	if(flag) {
		printf("NO\n");
		return 0;
	}
	int s = 0, t = sum + n + 1, idx = 1;
	dinic.init();
	for(int i = 2; i <= n; i++) {
		for(int j = i + 1; j <= n; j++) {
			if(G[i][j] > 0) {
				dinic.AddEdge(s, idx, G[i][j]);
				dinic.AddEdge(idx, sum + i, G[i][j]);
				dinic.AddEdge(idx, sum + j, G[i][j]);
				idx++;
			}
		}
	}
	for(int i = 2; i <= n; i++) {
		dinic.AddEdge(sum + i, t, score[1] - score[i]);
	}
	long long ans = dinic.solve(s, t);
	if(ans == cnt)
		printf("YES\n");
	else
		printf("NO\n");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值