QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#282323#1359. Setting MapsFAKUMARERWA 1ms5880kbC++143.9kb2023-12-11 19:05:192023-12-11 19:05:20

Judging History

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

  • [2023-12-11 19:05:20]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5880kb
  • [2023-12-11 19:05:19]
  • 提交

answer

#include <bits/stdc++.h>

const int MAXN = 10000;
const int MAXK = 15;
const long long INF  = 1e12;

using namespace std;

namespace Dinic {
	struct Edge {
		int to, nxt;
		long long F;
	} E[MAXN * MAXK << 1], P[MAXN << 1];
	
	int tot, H[MAXN * MAXK], tot_, H_[MAXN];
	
	inline int G (const int x, const int Floor, const bool Front) {
		return x + (((Floor) << 1) + Front) * MAXN;
		// a Floor has 2N Nodes (a Edge in OrgGraph will be Divided into two Nodes)
	}
	
	inline void Add_Edge (const int u, const int v, const long long w) {
		E[++ tot] = {v, H[u], w}, H[u] = tot;
		E[++ tot] = {u, H[v], 0}, H[v] = tot;
	}
	
	inline void add_edge (const int u, const int v, const int w) {
		P[++ tot_] = {v, H_[u], w}, H_[u] = tot_;
	//	P[++ tot_] = {u, H_[v], w}, H_[v] = tot_;
	}
	
	int Dep[MAXN * MAXK], Cur[MAXN * MAXK], N, K; // N is the total of Nodes
	int S, T;
	
	inline bool BFS () {
		for (int f = 0; f <= K + 1; ++ f)
		for (int i = 0; i <= N + 1; ++ i) 
		Dep[G (i, f, 0)] = Dep[G (i, f, 1)] = -1;
		
		queue <int> q;
		q.push (S), Dep[S] = 0, Cur[S] = H[S];
		int u;
		while (!q.empty ()) {
			u = q.front (), q.pop ();
			for (int i = H[u]; i; i = E[i].nxt) {
				if (Dep[E[i].to] == -1 && E[i].F > 0) {
					Cur[E[i].to] = H[E[i].to], Dep[E[i].to] = Dep[u] + 1;
					if (E[i].to == T) return 1;
					q.push (E[i].to);
				}
			}
		}
		
		return 0;
	}
		
	inline long long DFS (const int x, const long long MAXF) {
		if (x == T || MAXF == 0) return MAXF;
		long long F = 0;
		for (int i = Cur[x]; i && F < MAXF; i = E[i].nxt) {
			Cur[x] = i;
			if (Dep[E[i].to] == Dep[x] + 1 && E[i].F > 0) {
				long long TempF = DFS (E[i].to, min (E[i].F, MAXF - F));
				if (!TempF) Dep[E[i].to] = -1;
				F += TempF, E[i].F -= TempF, E[i + 1].F += TempF;
			}
		}
		return F;
	}
	
	inline long long dinic () {
		long long Ret = 0, F;
		while (BFS ()) {
			while ((F = DFS (S, INF))) 
				Ret += F;
		}
		return Ret;
	}
	
	inline void Print (const int x = 0) {
		if (x == 0)
			for (int i = 1; i <= tot; i += 2)
				cerr << "Edge - " << "From " << E[i + 1].to << " To " << E[i].to << " Cost " << E[i].F << endl;
		else if (x <= 110000) for (int i = H[x]; i; i = E[i].nxt) cerr << E[i].to << endl;
	
	
		for (int i = 1; i <= N; ++ i)
		for (int f = 1; f <= K; ++ f)
		cerr << G (i, f, 0) << ' ' << Dep[G (i, f, 0)] << ' ' << Dep[G (i, f, 1)] << endl;
	}
	
}

namespace Value {
	
	int N, M, K, S_, T_, Cnt;
	int U[MAXN], V[MAXN], C[MAXN];
	
	inline void Read () {
		cin >> N >> M >> K >> S_ >> T_;
		for (int i = 1; i <= N; ++ i) cin >> C[i];
		for (int i = 1; i <= M; ++ i) cin >> U[i] >> V[i];
	}
	
	inline void Build () {
		using namespace Dinic;
		for (int i = 1; i <= M; ++ i) add_edge (U[i], V[i], 1);
		
		for (int f = 1; f <= K; ++ f) {
			for (int i = 1; i <= N; ++ i) Add_Edge (G (i, f, 0), G (i, f, 1), C[i]);
			for (int i = 1; i <= M; ++ i) Add_Edge (G (U[i], f, 1), G (V[i], f, 0), INF);
			if (f != K)
			for (int i = 1; i <= N; ++ i) Add_Edge (G (i, f, 0), G (i, f + 1, 1), INF);
		}
		
		for (int f = 1; f <  K; ++ f) Add_Edge (G (T_, f, 1), G (T_, K, 1), INF);
		
		Dinic::S = G (S_, 1, 0), Dinic::T = G (T_, K, 1), Dinic::N = N, Dinic::K = K;
	}
	
	int dep[MAXN];
	
	inline void BFS () {
		using namespace Dinic;
		queue <int> q;
		q.push (S_), dep[S_] = 1;
		int u;
		while (!q.empty ()) {
			u = q.front (), q.pop ();
			for (int i = H_[u]; i; i = P[i].nxt) {
				if (dep[P[i].to]) continue;
				dep[P[i].to] = dep[u] + 1, q.push (P[i].to);
			}
		}
	}
	
	inline void Solve () {
		Build ();
		
		BFS ();
		
		if (dep[T_] < K) cout << -1 << endl;
		else {
			long long Ans = Dinic::dinic ();
			
			if (Ans > INF) cout << -1 << endl;
			else cout << Ans << endl;
		}
	}
	
}


int main () {

//	freopen ("apers.in", "r", stdin);
//	freopen ("apers.out", "w", stdout);
	
	Value::Read ();
	
	Value::Solve ();
	
	return 0;
}

详细

Test #1:

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

input:

3 2 5
1 3
1 60 35
1 2
2 3

output:

-1

result:

ok answer = IMPOSSIBLE

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 3604kb

input:

7 11 1
1 7
100 5 7 16 11 12 100
1 2
1 3
1 4
1 5
2 3
2 6
3 6
4 3
4 7
5 7
6 7

output:

39

result:

wrong answer Integer parameter [name=p; number of vertices] equals to 39, violates the range [-1, 7]