QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#60413#4437. Link with Runningqinjianbin#TL 0ms0kbC++171.5kb2022-11-04 15:36:392022-11-04 15:36:40

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:36:40]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2022-11-04 15:36:39]
  • 提交

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];

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--)
	{
		scanf("%d%d", &n, &m);
		_for(i, 1, n) g[i].clear();
		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) dp[i] = deg[i] = vis[i] = 0;
		deque<int> q;
		q.push_front(1);
		vis[1] = 1;

		while(!q.empty())
		{
			int u = q.front(); q.pop_front();
			vis[u] = 0;
			for(auto [v, w, p]: g[u])
				if(d[v] == d[u] + w)
					if(dp[v] < dp[u] + p)
					{
						dp[v] = dp[u] + p;
						if(!vis[v]) 
						{
							if(!q.empty() && d[v] > d[q.front()]) q.push_front(v);
							else q.push_back(v);
							vis[v] = 1;
						}
					}
		}
		printf("%lld\n", dp[n]);
	}

	return 0; 
} 

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

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:


result: