Submission #880117


Source Code Expand

#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
using namespace std;

const int inf = 17070509, maxN = int(1e5)+5;

int n, m;
int res[maxN+10];
map<int, int> D[maxN+10];
vector<pair<int, int>> graph[maxN+10];

int dijkstra(int node)
{
	set<pair<int, pair<int, int>>> Q;

	for(auto it: graph[node]) Q.insert({1, {node, it.second}});

	pair<int, pair<int, int>> top;
	while(!Q.empty())
	{
		top = *Q.begin();
		Q.erase(Q.begin());

		for(auto it: graph[top.second.first])
		{
			if(D[it.first].find(it.second) == D[it.first].end())
			{
				D[it.first][it.second] = top.first+(top.second.second!=it.second);
				Q.insert({top.first+(top.second.second!=it.second), it});
				res[it.first] = min(res[it.first], top.first+(top.second.second!=it.second));
			}
			else if(D[it.first][it.second] > top.first+(top.second.second!=it.second))
			{
				Q.erase({D[it.first][it.second], it});
				D[it.first][it.second] = top.first+(top.second.second!=it.second);
				Q.insert({top.first+(top.second.second!=it.second), it});
				res[it.first] = min(res[it.first], top.first+(top.second.second!=it.second));
			}
		}
	}
	return res[n-1];
}

int main(void)
{
	int u, v, c;
	scanf("%d%d", &n, &m);
	for(int i = 0;i < n;i++) res[i] = inf;
	for(int i = 0;i < m;i++)
	{
		scanf("%d%d%d", &u, &v, &c);
		u--; v--;
		graph[u].push_back({v, c});
		graph[v].push_back({u, c});
	}

	int res = dijkstra(0);
	if(res == inf) printf("-1\n");
	else printf("%d\n", res);
}

Submission Info

Submission Time
Task E - Snuke's Subway Trip
User NibNalin
Language C++14 (GCC 5.4.1)
Score 0
Code Size 1544 Byte
Status TLE
Exec Time 3157 ms
Memory 37876 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:50:23: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &n, &m);
                       ^
./Main.cpp:54:30: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d", &u, &v, &c);
                              ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 600
Status AC
AC × 47
TLE × 9
Set Name Test Cases
Sample
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, w1.txt, w10.txt, w11.txt, w12.txt, w13.txt, w14.txt, w15.txt, w16.txt, w17.txt, w18.txt, w2.txt, w3.txt, w4.txt, w5.txt, w6.txt, w7.txt, w8.txt, w9.txt
Case Name Status Exec Time Memory
01.txt AC 15 ms 7296 KB
02.txt TLE 3155 ms 20608 KB
03.txt AC 870 ms 36352 KB
04.txt AC 161 ms 13952 KB
05.txt AC 616 ms 30208 KB
06.txt AC 161 ms 19456 KB
07.txt AC 257 ms 20480 KB
08.txt AC 857 ms 36352 KB
09.txt AC 300 ms 22272 KB
10.txt AC 310 ms 22272 KB
11.txt AC 548 ms 29948 KB
12.txt AC 116 ms 11256 KB
13.txt AC 342 ms 19448 KB
14.txt AC 470 ms 28672 KB
15.txt AC 219 ms 22268 KB
16.txt AC 371 ms 26624 KB
17.txt AC 604 ms 30840 KB
18.txt AC 165 ms 13564 KB
19.txt AC 373 ms 26612 KB
20.txt AC 813 ms 32640 KB
21.txt TLE 3155 ms 16124 KB
22.txt TLE 3157 ms 34552 KB
23.txt AC 797 ms 33536 KB
24.txt AC 432 ms 28412 KB
25.txt AC 127 ms 11256 KB
26.txt AC 293 ms 19448 KB
27.txt AC 482 ms 34048 KB
28.txt AC 478 ms 31612 KB
29.txt AC 484 ms 32512 KB
30.txt AC 430 ms 25336 KB
31.txt AC 430 ms 20860 KB
32.txt AC 295 ms 26612 KB
33.txt AC 478 ms 33408 KB
34.txt AC 952 ms 22268 KB
35.txt AC 96 ms 11256 KB
36.txt AC 262 ms 13688 KB
37.txt AC 950 ms 22272 KB
38.txt AC 1010 ms 22268 KB
sample_01.txt AC 14 ms 7296 KB
sample_02.txt AC 14 ms 7296 KB
sample_03.txt AC 14 ms 7296 KB
w1.txt AC 350 ms 21220 KB
w10.txt AC 265 ms 16640 KB
w11.txt TLE 3154 ms 11096 KB
w12.txt TLE 3154 ms 12160 KB
w13.txt AC 1540 ms 19840 KB
w14.txt AC 444 ms 23424 KB
w15.txt AC 145 ms 11224 KB
w16.txt AC 148 ms 11264 KB
w17.txt AC 167 ms 12032 KB
w18.txt AC 224 ms 16000 KB
w2.txt AC 358 ms 24668 KB
w3.txt AC 435 ms 25844 KB
w4.txt AC 246 ms 19828 KB
w5.txt TLE 3157 ms 37876 KB
w6.txt AC 665 ms 29812 KB
w7.txt TLE 3154 ms 12564 KB
w8.txt TLE 3154 ms 11520 KB
w9.txt TLE 3155 ms 24320 KB