제출 #118799

#제출 시각아이디문제언어결과실행 시간메모리
118799wilwxkCommuter Pass (JOI18_commuter_pass)C++11
100 / 100
1470 ms43128 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int MAXN=1e5+5;
const ll INF=1e18;
vector<int> g[MAXN], pes[MAXN];
vector<int> dg[5][MAXN];
set<pair<ll, int> > s;
tuple<int, int, int> aresta[MAXN*2];
ll dist[5][MAXN], dp[2][MAXN];
int gin[2][MAXN];
int n, m, ori, dest, x, y;

void dijkstra(int ori, int k) {
	for(int i=1; i<=n; i++) dist[k][i]= i==ori ? 0 : INF, s.insert({dist[k][i], i});
	while(s.size()) {
		int cur=s.begin()->second; s.erase(s.begin());
		for(int i=0; i<g[cur].size(); i++) {
			int viz=g[cur][i]; ll peso=pes[cur][i];
			if(dist[k][viz]>dist[k][cur]+peso) {
				s.erase({dist[k][viz], viz});
				dist[k][viz]=dist[k][cur]+peso;
				s.insert({dist[k][viz], viz});
			}
		}
	}
}

void bfs(int k) {
	queue<int> qu;
	for(int i=1; i<=n; i++) if(gin[k][i]==0) qu.push(i);
	while(qu.size()) {
		int cur=qu.front(); qu.pop();
		for(auto viz : dg[k][cur]) {
			gin[k][viz]--;
			if(gin[k][viz]==0) qu.push(viz);
			dp[k][viz]=min(dp[k][viz], dp[k][cur]);
		}
	}
}

int main() {
	scanf("%d %d", &n, &m);
	scanf("%d %d", &ori, &dest);
	scanf("%d %d", &x, &y);
	for(int i=1; i<=m; i++) {
		int a, b, c; scanf("%d %d %d", &a, &b, &c);
		aresta[i]={a, b, c};
		g[a].push_back(b); g[b].push_back(a);
		pes[a].push_back(c); pes[b].push_back(c);
	}
	dijkstra(ori, 0); dijkstra(dest, 1);
	dijkstra(x, 2); dijkstra(y, 3);
	for(int i=1; i<=n; i++) dp[0][i]=dp[1][i]=dist[2][i];

	for(int i=1; i<=m; i++) {
		int a, b, c; tie(a, b, c)=aresta[i];
		if(dist[0][a]+dist[1][b]+c!=dist[0][dest]&&dist[0][b]+dist[1][a]+c!=dist[0][dest]) continue;
		if(dist[0][a]+c==dist[0][b]) {
			dg[0][a].push_back(b); gin[0][b]++;
			dg[1][b].push_back(a); gin[1][a]++;
		}
		if(dist[0][b]+c==dist[0][a]) {
			dg[0][b].push_back(a); gin[0][a]++;
			dg[1][a].push_back(b); gin[1][b]++;
		}
	}

	bfs(0); bfs(1);
	// for(int i=1; i<=n; i++) printf("%d: %lld %lld // %lld %lld\n", i, dist[0][i], dist[1][i], dist[2][i], dist[3][i]);
	// for(int i=1; i<=n; i++) printf("dp[%d]= (%lld ; %lld)\n", i, dp[0][i], dp[1][i]);

	ll respf=INF;
	for(int i=1; i<=n; i++) respf=min(respf, dist[3][i]+min(dp[0][i], dp[1][i]));

	printf("%lld\n", respf);
}

컴파일 시 표준 에러 (stderr) 메시지

commuter_pass.cpp: In function 'void dijkstra(int, int)':
commuter_pass.cpp:19:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=0; i<g[cur].size(); i++) {
                ~^~~~~~~~~~~~~~
commuter_pass.cpp: In function 'int main()':
commuter_pass.cpp:44:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &m);
  ~~~~~^~~~~~~~~~~~~~~~~
commuter_pass.cpp:45:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &ori, &dest);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~
commuter_pass.cpp:46:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &x, &y);
  ~~~~~^~~~~~~~~~~~~~~~~
commuter_pass.cpp:48:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int a, b, c; scanf("%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...