Submission #881993


Source Code Expand

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second

typedef pair<int,int> pi;

const int INF=12345678;
const int N=101010;
const int M=505050;

struct edge{int to, c;};

// 与えられる辺情報
vector<edge> G[N];
// 与えられる辺情報を元に拡張
vector<edge> exG[M];
//駅iにたどり着く直前に会社jの路線を使ったときにかかる料金の最小値
int dist[M];

int main()
{
    int n,m;
    scanf(" %d %d", &n, &m);

    // 拡張された頂点を連続する整数に変換
    map<pi,int> convert_pi_to_id;
    int idx=0;

    rep(i,m)
    {
        int p,q,company;
        scanf(" %d %d %d", &p, &q, &company);
        G[p].pb(edge{q,company});
        G[q].pb(edge{p,company});

        // 同じ鉄道会社間にはコスト0の辺を張る
        if(convert_pi_to_id.find(pi(p,company)) == convert_pi_to_id.end())
        {
            convert_pi_to_id[pi(p,company)] = idx++;
        }
        if(convert_pi_to_id.find(pi(q,company)) == convert_pi_to_id.end())
        {
            convert_pi_to_id[pi(q,company)] = idx++;
        }

        int id_p = convert_pi_to_id[pi(p,company)];
        int id_q = convert_pi_to_id[pi(q,company)];

        exG[id_p].pb(edge{id_q,0});
        exG[id_q].pb(edge{id_p,0});
    }

    // 駅iに関して
    for(int i=1; i<=n; ++i)
    {
        set<int> connected;
        rep(j,G[i].size()) connected.insert(G[i][j].c);

        convert_pi_to_id[pi(i,0)] = idx++;

        for(const int &company : connected)
        {
            int ent_id  = convert_pi_to_id[pi(i,company)];
            int exit_id = convert_pi_to_id[pi(i,0)];
            // 出場
            exG[ent_id].pb(edge{exit_id,0});
            // 入場
            exG[exit_id].pb(edge{ent_id,1});
        }
    }

    // dijkstra
    priority_queue<pi,vector<pi>,greater<pi>> pq;
    fill(dist,dist+M,INF);
    int start = convert_pi_to_id[pi(1,0)];
    dist[start]=0;
    pq.push(pi(0,start));
    while(!pq.empty())
    {
        pi now = pq.top();
        pq.pop();

        if(dist[now.se] < now.fi) continue;

        rep(i,exG[now.se].size())
        {
            edge e = exG[now.se][i];
            if(dist[e.to] > dist[now.se]+e.c)
            {
                dist[e.to] = dist[now.se]+e.c;
                pq.push(pi(dist[e.to], e.to));
            }
        }
    }

    int goal = convert_pi_to_id[pi(n,0)];
    int ans=dist[goal];
    if(ans==INF) ans=-1;
    cout << ans << endl;
    return 0;
}

Submission Info

Submission Time
Task E - Snuke's Subway Trip
User imulan
Language C++14 (GCC 5.4.1)
Score 600
Code Size 2815 Byte
Status AC
Exec Time 1617 ms
Memory 76148 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:30:28: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf(" %d %d", &n, &m);
                            ^
./Main.cpp:39:45: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
         scanf(" %d %d %d", &p, &q, &company);
                                             ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status AC
AC × 56
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 30 ms 16384 KB
02.txt AC 813 ms 37504 KB
03.txt AC 1617 ms 72440 KB
04.txt AC 1384 ms 71040 KB
05.txt AC 1378 ms 61944 KB
06.txt AC 624 ms 46592 KB
07.txt AC 747 ms 47488 KB
08.txt AC 1530 ms 72312 KB
09.txt AC 800 ms 50560 KB
10.txt AC 790 ms 50560 KB
11.txt AC 835 ms 48884 KB
12.txt AC 197 ms 24820 KB
13.txt AC 497 ms 34168 KB
14.txt AC 773 ms 48508 KB
15.txt AC 620 ms 42612 KB
16.txt AC 777 ms 46456 KB
17.txt AC 878 ms 49400 KB
18.txt AC 327 ms 27772 KB
19.txt AC 763 ms 46316 KB
20.txt AC 877 ms 51452 KB
21.txt AC 1381 ms 70732 KB
22.txt AC 896 ms 52988 KB
23.txt AC 1055 ms 59132 KB
24.txt AC 754 ms 50548 KB
25.txt AC 181 ms 24304 KB
26.txt AC 422 ms 33904 KB
27.txt AC 801 ms 52340 KB
28.txt AC 755 ms 50804 KB
29.txt AC 867 ms 51704 KB
30.txt AC 728 ms 49392 KB
31.txt AC 422 ms 34424 KB
32.txt AC 640 ms 45672 KB
33.txt AC 860 ms 52348 KB
34.txt AC 639 ms 43384 KB
35.txt AC 170 ms 24432 KB
36.txt AC 358 ms 28920 KB
37.txt AC 650 ms 44284 KB
38.txt AC 644 ms 43128 KB
sample_01.txt AC 32 ms 16384 KB
sample_02.txt AC 28 ms 16384 KB
sample_03.txt AC 28 ms 16384 KB
w1.txt AC 751 ms 46036 KB
w10.txt AC 1266 ms 65664 KB
w11.txt AC 284 ms 24392 KB
w12.txt AC 425 ms 27520 KB
w13.txt AC 860 ms 43008 KB
w14.txt AC 1243 ms 62464 KB
w15.txt AC 161 ms 24520 KB
w16.txt AC 241 ms 25216 KB
w17.txt AC 356 ms 26624 KB
w18.txt AC 611 ms 35072 KB
w2.txt AC 781 ms 46280 KB
w3.txt AC 1037 ms 53364 KB
w4.txt AC 687 ms 45416 KB
w5.txt AC 1498 ms 76148 KB
w6.txt AC 1211 ms 61300 KB
w7.txt AC 1417 ms 61820 KB
w8.txt AC 1393 ms 60672 KB
w9.txt AC 1295 ms 62336 KB