QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#153783#7048. Delivery RoutePetroTarnavskyi#TL 4ms14500kbC++172.5kb2023-08-30 23:14:252023-08-30 23:14:26

Judging History

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

  • [2023-08-30 23:14:26]
  • 评测
  • 测评结果:TL
  • 用时:4ms
  • 内存:14500kb
  • [2023-08-30 23:14:25]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a) - 1; i >= (b); i--)
#define FILL(a, b) memset(a, b, sizeof(a))
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second


typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;

const int N = 1 << 17;
vector<PII> g[2][N];

int has_path[N];
void dfs1(int v)
{
	has_path[v] = 1;
	FOR(t, 0, 2)
	{
		for(auto e : g[t][v])
		{
			int to = e.F;
			if(!has_path[to])
			{
				dfs1(to);	
			}
		}
	}
}
int component[N];
int used[N];
VI verts[N];
int cntin[N];

void dfs2(int v, int comp)
{
	used[v] = 1;
	component[v] = comp;
	verts[comp].PB(v);
	
	for(auto e : g[0][v])
	{
		int to = e.F;
		if(!used[to])
		{
			dfs2(to, comp);
		}
	}	
}

const int INF = 1e9;
int d[N];

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n, x, y, s;
	cin >> n >> x >> y >> s;
	s--;
	FOR(t, 0, 2)
	{
		FOR(i, 0, x)
		{
			int a, b, w;
			cin >> a >> b >> w;
			a--; b--;
			g[t][a].PB(MP(b, w));
			if(t == 0)
			{
				g[t][b].PB(MP(a, w));
			}
		}
		swap(x, y);
	}
	dfs1(s);
	int cnt_comp = 0;
	FOR(i, 0, n)
	{
		if(has_path[i] && !used[i])
		{
			dfs2(i, cnt_comp++);
		}
	}
	FOR(v, 0, n)
	{
		if(has_path[v])
		{
			for(auto e : g[1][v])
			{
				int to = e.F;
				assert(component[v] != component[to]);
				cntin[component[to]]++;
			}
		}
	}
	FOR(v, 0, n)
		d[v] = INF;
	set<PII> q;
	FOR(i, 0, cnt_comp)
	{
		if(cntin[i] == 0)
		{
			for(int v : verts[i])
			{
				q.insert(MP(INF, v));
			}
		}
	}
	q.erase(MP(INF, s));
	d[s] = 0;
	q.insert(MP(0, s));
	while(SZ(q))
	{
		int v = q.begin()->S;
		q.erase(q.begin());
		FOR(t, 0, 2)
		{
			for(auto e : g[t][v])
			{
				int to = e.F;
				int w = e.S;
				int comp = component[to];
				
				if(t)
				{
					cntin[comp]--;
					assert(cntin[comp] >= 0);
					if(cntin[comp] == 0)
					{
						for(int u : verts[comp])
						{
							q.insert(MP(d[u], u));
						}
					}
				}
				if(d[to] < d[v] + w)
				{
					continue;
				}
				
				if(cntin[comp] == 0)
				{	
					q.erase(MP(d[to], to));
					d[to] = d[v] + w;
					q.insert(MP(d[to], to));
				}
				d[to] = d[v] + w;
			}
		}
	}
	
	FOR(i, 0, n)
	{
		if(!has_path[i])
		{
			cout << "NO PATH\n";
		}
		else
			cout << d[i] << "\n";
		
	}
	
	
	return 0;
}




詳細信息

Test #1:

score: 100
Accepted
time: 3ms
memory: 14500kb

input:

6 3 3 4
1 2 5
3 4 5
5 6 10
3 5 -100
4 6 -100
1 3 -10

output:

NO PATH
NO PATH
5
0
-95
-100

result:

ok 6 lines

Test #2:

score: 0
Accepted
time: 4ms
memory: 14288kb

input:

100 500 500 4
13 20 329
41 10 63
100 90 297
15 79 163
24 67 747
29 68 443
73 98 565
10 41 921
79 62 973
31 85 270
25 29 672
34 43 391
30 92 604
58 82 90
28 16 460
53 63 350
91 98 260
70 22 232
5 36 335
37 32 339
4 48 940
85 1 233
95 78 323
62 79 688
49 57 576
10 54 103
33 78 541
88 22 171
4 48 408
2...

output:

-3619
-3536
NO PATH
0
NO PATH
-7205
-6588
-2243
-2898
-691
-6755
-710
-4236
-3929
-941
-4827
-4796
NO PATH
-6827
-4246
-4759
-1991
-5025
-1522
-6643
-6453
-3084
-4740
-6448
-5552
-3800
-2700
-7006
-3915
-433
NO PATH
-2767
NO PATH
158
-2524
-628
-7166
-3859
-2838
-2168
-7106
-2726
112
-3844
544
-3860...

result:

ok 100 lines

Test #3:

score: -100
Time Limit Exceeded

input:

50 100 50 24
26 39 94
44 42 47
15 39 28
10 25 37
28 12 3
36 22 34
9 36 74
28 12 35
25 22 63
48 7 86
29 37 69
9 10 4
36 9 31
28 34 54
6 1 89
33 25 17
36 10 47
5 35 9
7 14 65
44 50 96
31 1 35
37 29 4
28 34 27
35 18 74
34 8 37
13 17 2
19 48 47
20 43 98
26 16 91
13 29 63
20 43 48
5 35 54
39 15 69
10 33 ...

output:


result: