QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#60418#4437. Link with Runningqinjianbin#AC ✓1000ms60588kbC++172.0kb2022-11-04 15:49:192022-11-04 15:49:22

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-11-04 15:49:22]
  • 评测
  • 测评结果:AC
  • 用时:1000ms
  • 内存:60588kb
  • [2022-11-04 15:49:19]
  • 提交

answer

#include<bits/stdc++.h>
#define rep(i, a, b) for(int i = (a); i < (b); i++) 
#define _for(i, a, b) for(int i = (a); i <= (b); i++) 
using namespace std;

typedef long long ll;
const int N = 1e5 + 10;
struct Edge 
{ 
	int v; ll w, p; 
	bool operator < (const Edge& rhs) const
	{
		return w > rhs.w;
	}
};
vector<Edge> g[N];
int deg[N], vis[N], n, m;
ll d[N], dp[N];
int dfn[N], low[N], st[N], co[N], top, cnt, id;
vector<pair<int, int>> G[N];

void dfs(int u)
{
	dfn[u] = low[u] = ++cnt;
	st[++top] = u;

	for(auto [v, w, p]: g[u])
		if(d[v] == d[u] + w)
		{
			if(!dfn[v])
			{
				dfs(v);
				low[u] = min(low[u], low[v]);
			}
			else if(!co[v]) low[u] = min(low[u], dfn[v]);
		}
	
	if(low[u] == dfn[u])
	{
		id++;
		while(1)
		{
			co[st[top--]] = id;
			if(st[top + 1] == u) break;
		}
	}
}

void solve()
{
	_for(i, 1, n) d[i] = 1e18;
	d[1] = 0;
	priority_queue<Edge> q;
	q.push({1, d[1], 0});

	while(!q.empty())
	{
		Edge x = q.top(); q.pop();
		int u = x.v;
		if(d[u] != x.w) continue;

		for(auto [v, w, p]: g[u])
			if(d[v] > d[u] + w)
			{
				d[v] = d[u] + w;
				q.push({v, d[v], 0});
			}
	}
}

int main()
{
	int T; scanf("%d", &T);
	while(T--)
	{
		cnt = top = id = 0;
		scanf("%d%d", &n, &m);
		_for(i, 1, n) g[i].clear(), G[i].clear(), low[i] = dfn[i] = co[i] = dp[i] = deg[i] = 0;
		while(m--)
		{
			int u, v, w, p;
			scanf("%d%d%d%d", &u, &v, &w, &p);
			g[u].push_back({v, w, p});
		}

		solve();
		printf("%lld ", d[n]);

		_for(i, 1, n)
			if(!dfn[i])
				dfs(i);
		_for(u, 1, n)
			for(auto [v, w, p]: g[u])
				if(d[v] == d[u] + w)
					if(co[u] != co[v])
					{
						G[co[u]].push_back({co[v], p});
						deg[co[v]]++;
					}
		
		queue<int> q;
		_for(i, 1, id)
			if(!deg[i])
				q.push(i);
		while(!q.empty())
		{
			int u = q.front(); q.pop();
			for(auto [v, p]: G[u])
			{
				dp[v] = max(dp[v], dp[u] + p);
				if(--deg[v]== 0) q.push(v);
			}
		}
		printf("%lld\n", dp[co[n]]);
	}

	return 0; 
} 

详细

Test #1:

score: 100
Accepted
time: 1000ms
memory: 60588kb

input:

12
100000 200000
1 2 838279516 902819511
1 3 293478832 513256010
2 4 682688353 204481674
2 5 360092507 651108247
5 6 519851939 323803002
6 7 675439277 205804465
7 8 419167205 386168059
6 9 140767493 382483305
9 10 558115401 613738466
9 11 902235661 744659643
9 12 851394758 1720015
12 13 635355827 46...

output:

5927443549 11285847934
2529348 325344428756
2522027 438209666288
250100947 25049026205784
249512452 24966236662852
0 9535132634
0 25375698217
0 1000000000
0 1
0 2
0 1
0 1

result:

ok 12 lines