QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#287797#3855. Regional developmentLaStataleBlue#RE 1ms3528kbC++232.5kb2023-12-20 23:45:342023-12-20 23:45:35

Judging History

你现在查看的是最新测评结果

  • [2023-12-20 23:45:35]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3528kb
  • [2023-12-20 23:45:34]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;


using pi = pair<int,int>;
#define sz(x) int((x).size())
template<class F> struct Dinic {
	struct Edge { int to, rev; F cap; };
	int N; vector<vector<Edge>> adj;
	void init(int _N) { N = _N; adj.resize(N); } //0-based
	pi ae(int a, int b, F cap, F rcap = 0) {
		assert(min(cap,rcap) >= 0); // saved me > once
		adj[a].push_back({b,sz(adj[b]),cap});
		adj[b].push_back({a,sz(adj[a])-1,rcap});
		return {a,sz(adj[a])-1};
	}
	F edgeFlow(pi loc) { // get flow along original edge
		const Edge& e = adj.at(loc.first).at(loc.second);
		return adj.at(e.to).at(e.rev).cap;
	}
	vector<int> lev, ptr;
	bool bfs(int s,int t){//level=shortest dist from source
		lev = ptr = vector<int>(N);
		lev[s] = 1; queue<int> q({s});
		while (sz(q)) { int u = q.front(); q.pop();
			for(auto& e: adj[u]) if (e.cap && !lev[e.to]) {
				q.push(e.to), lev[e.to] = lev[u]+1;
				if (e.to == t) return 1;
			}
		}
		return 0;
	}
	F dfs(int v, int t, F flo) {
		if (v == t) return flo;
		for (int& i = ptr[v]; i < sz(adj[v]); i++) {
			Edge& e = adj[v][i];
			if (lev[e.to]!=lev[v]+1||!e.cap) continue;
			if (F df = dfs(e.to,t,min(flo,e.cap))) {
				e.cap -= df; adj[e.to][e.rev].cap += df;
				return df; } // saturated $\geq1$ one edge
		}
		return 0;
	}
	F maxFlow(int s, int t) {
		F tot = 0; while (bfs(s,t)) while (F df =
			dfs(s,t,numeric_limits<F>::max())) tot += df;
		return tot;
	}
};
#define int long long

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int n,r,m;
	cin>>n>>r>>m;
	
	Dinic<int> ds;
	int source = n, sink = n+1;
	ds.init(n+2);
	
	vector<int> a(r),b(r),c(r);
	vector<int> sum(n,0);
	
	vector mat(n,vector(n,0));
	
	for(int i=0;i<r;i++){
		cin>>a[i]>>b[i]>>c[i];
		a[i]--;
		b[i]--;
		
		sum[a[i]]-=c[i];
		sum[b[i]]+=c[i];
		
		mat[a[i]][b[i]]=m-1-c[i];
		mat[b[i]][a[i]]=m-1+c[i];
		
		ds.ae(a[i],b[i],m-1-c[i]);
		ds.ae(b[i],a[i],m-1+c[i]);
	}
	
	int tot=0,tot2=0;
	for(int i=0;i<n;i++){
		if(sum[i]<0){
			ds.ae(i,sink,-sum[i]);
			tot2+=-sum[i];
		}
		else{
			tot+=sum[i];
			ds.ae(source,i,sum[i]);
		}
	}
	
	if(tot!=tot2 || ds.maxFlow(source,sink)!=tot){
		cout<<"IMPOSSIBLE\n";
	}else{
		
		for(int i=0;i<n;i++){
			for(auto j : ds.adj[i]){
				if(j.to!=source && j.to!=sink){
					mat[i][j.to]-=j.cap;
					//cout<<i<<" "<<j.to<<" "<<j.cap<<"\n";
				}
			}
		}
		
		for(int i=0;i<r;i++){
			int x = c[i]+mat[a[i]][b[i]];
			assert(x!=0);
			cout<<c[i]+mat[a[i]][b[i]]<<"\n";
		}
	}
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3528kb

input:

10 23 3
2 10 1
2 6 1
6 9 1
7 9 1
6 7 1
3 6 1
1 3 1
1 6 2
6 10 1
2 9 1
4 9 2
4 7 2
3 10 2
3 9 1
6 8 1
7 8 2
3 5 1
5 9 1
8 9 2
3 8 2
1 5 2
1 4 1
5 10 2

output:

1
1
1
1
1
1
-2
-1
-2
-2
-1
2
-1
-2
1
2
1
1
2
-1
2
1
2

result:

ok correct plan

Test #2:

score: -100
Runtime Error

input:

100 1297 11
27 34 5
7 34 6
7 90 3
80 90 6
37 80 6
37 48 5
47 48 6
47 86 5
56 86 6
56 84 5
63 84 6
38 63 6
66 91 8
12 91 6
12 15 5
15 22 1
22 87 5
46 87 6
46 63 10
63 87 8
71 87 6
65 71 6
38 65 1
38 67 4
29 67 6
9 29 6
9 21 5
16 21 6
16 89 2
89 98 5
4 98 6
4 13 4
13 33 5
14 33 6
14 66 10
66 86 10
57 ...

output:


result: