Submission #103359

#TimeUsernameProblemLanguageResultExecution timeMemory
103359tincamateiCommuter Pass (JOI18_commuter_pass)C++14
100 / 100
467 ms21636 KiB
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 100000;
const long long INF = 1LL << 60;

vector<pair<int, int> > graph[1+MAX_N];

long long dist[2][1+MAX_N], dists[1+MAX_N];
long long dp[1+MAX_N][4];
bool viz[1+MAX_N];

void dijkstra(int n, int src, long long *dist) {
	priority_queue<pair<long long, int> > pq;

	for(int i = 1; i <= n; ++i)
		dist[i] = INF;
	
	dist[src] = 0;
	pq.push(make_pair(0, src));
	
	while(!pq.empty()) {
		int nod = pq.top().second;
		long long cost = -pq.top().first;
		pq.pop();

		if(cost == dist[nod]) {
			for(auto it: graph[nod])
				if(cost + it.second < dist[it.first]) {
					dist[it.first] = cost + it.second;
					pq.push(make_pair(-cost - it.second, it.first));
				}
		}
	}
}

void computeDp(int nod) {
	if(!viz[nod]) {
		viz[nod] = true;
		dp[nod][1] = dist[0][nod];
		dp[nod][2] = dist[1][nod];
		dp[nod][3] = dist[0][nod] + dist[1][nod];

		for(auto it: graph[nod])
			if(dists[nod] - it.second == dists[it.first]) {
				computeDp(it.first);
				dp[nod][1] = min(dp[nod][1], dp[it.first][1]);
				dp[nod][2] = min(dp[nod][2], dp[it.first][2]);
				dp[nod][3] = min(min(min(dp[nod][3], 
				                         dp[it.first][1] + dist[1][nod]),
				                         dp[it.first][2] + dist[0][nod]),
				                         dp[it.first][3]);
			}
	}
}

int main() {
#ifdef HOME
	FILE *fin = fopen("input.in", "r");
	FILE *fout = fopen("output.out", "w");
#else
	FILE *fin = stdin;
	FILE *fout = stdout;
#endif

	priority_queue<int> pq;

	int n, m, s, t, u, v;
	int a, b, c;

	fscanf(fin, "%d%d%d%d%d%d", &n, &m, &s, &t, &u, &v);
	for(int i = 0; i < m; ++i) {
		fscanf(fin, "%d%d%d", &a, &b, &c);
		graph[a].push_back(make_pair(b, c));
		graph[b].push_back(make_pair(a, c));
	}

	dijkstra(n, u, dist[0]);
	dijkstra(n, v, dist[1]);
	dijkstra(n, s, dists);
	
	computeDp(t);
	
	fprintf(fout, "%lld", min(dist[0][v], dp[t][3]));

	fclose(fin);
	fclose(fout);
	return 0;
}

Compilation message (stderr)

commuter_pass.cpp: In function 'int main()':
commuter_pass.cpp:72:8: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  fscanf(fin, "%d%d%d%d%d%d", &n, &m, &s, &t, &u, &v);
  ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
commuter_pass.cpp:74:9: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   fscanf(fin, "%d%d%d", &a, &b, &c);
   ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...