답안 #263546

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
263546 2020-08-13T19:19:51 Z MKopchev Olympic Bus (JOI20_ho_t4) C++14
100 / 100
758 ms 4048 KB
#include<bits/stdc++.h>
using namespace std;

const int nmax=200+5,mmax=50000+5;

const long long inf=1e16+42;

int n,m;

struct edges
{
    int from,to,cost,to_rev;
};
edges inp[mmax];

vector< pair<int/*dist*/,int/*to*/> > adj[nmax];

long long dist[nmax];

priority_queue< pair<long long/*dist*/,int/*node*/> > pq,idle;

long long dij(int from,int to)
{
    memset(dist,-1,sizeof(dist));

    pq=idle;

    pq.push({0,from});

    while(dist[to]==-1&&pq.size())
    {
        pair<long long/*dist*/,int/*node*/> tp=pq.top();
        pq.pop();

        tp.first=-tp.first;

        if(dist[tp.second]!=-1)continue;

        dist[tp.second]=tp.first;

        for(auto k:adj[tp.second])
            if(dist[k.second]==-1)pq.push({-(tp.first+k.first),k.second});
    }

    //for(int i=1;i<=n;i++)cout<<dist[i]<<" ";cout<<endl;

    if(dist[to]==-1)return inf;
    return dist[to];
}
long long solve(int which)
{
    long long ret=inp[which].to_rev;

    for(int i=1;i<=n;i++)adj[i]={};

    for(int i=1;i<=m;i++)
        if(i!=which)adj[inp[i].from].push_back({inp[i].cost,inp[i].to});
        else adj[inp[i].to].push_back({inp[i].cost,inp[i].from});

    ret=ret+dij(1,n);

    ret=ret+dij(n,1);

    //cout<<"which= "<<which<<" ret= "<<ret<<endl;

    return ret;
}

long long floyd[nmax][nmax];

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

    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            floyd[i][j]=inf;

    for(int i=1;i<=n;i++)
        floyd[i][i]=0;

    for(int i=1;i<=m;i++)
    {
        scanf("%i%i%i%i",&inp[i].from,&inp[i].to,&inp[i].cost,&inp[i].to_rev);
        floyd[inp[i].from][inp[i].to]=min(floyd[inp[i].from][inp[i].to],1LL*inp[i].cost);
    }

    for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                floyd[i][j]=min(floyd[i][j],floyd[i][k]+floyd[k][j]);

    /*
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)cout<<i<<" "<<j<<" -> "<<floyd[i][j]<<endl;
    */

    long long outp=inf;

    outp=min(outp,floyd[1][n]+floyd[n][1]);

    for(int i=1;i<=m;i++)
    {
        long long low_path_1=min(floyd[1][n],floyd[1][inp[i].to]+inp[i].cost+floyd[inp[i].from][n]);

        long long low_path_2=min(floyd[n][1],floyd[n][inp[i].to]+inp[i].cost+floyd[inp[i].from][1]);

        //cout<<i<<" -> "<<low_path_1<<" "<<low_path_2<<" "<<inp[i].to_rev<<" "<<outp<<endl;

        if(low_path_1+low_path_2+inp[i].to_rev<outp)outp=min(outp,solve(i));
    }
    if(outp>=inf)printf("-1\n");
    else printf("%i\n",outp);

    return 0;
}

Compilation message

ho_t4.cpp: In function 'int main()':
ho_t4.cpp:113:19: warning: format '%i' expects argument of type 'int', but argument 2 has type 'long long int' [-Wformat=]
  113 |     else printf("%i\n",outp);
      |                  ~^    ~~~~
      |                   |    |
      |                   int  long long int
      |                  %lli
ho_t4.cpp:73:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   73 |     scanf("%i%i",&n,&m);
      |     ~~~~~^~~~~~~~~~~~~~
ho_t4.cpp:84:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   84 |         scanf("%i%i%i%i",&inp[i].from,&inp[i].to,&inp[i].cost,&inp[i].to_rev);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 640 KB Output is correct
2 Correct 10 ms 640 KB Output is correct
3 Correct 10 ms 640 KB Output is correct
4 Correct 12 ms 640 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 10 ms 640 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 15 ms 768 KB Output is correct
11 Correct 27 ms 768 KB Output is correct
12 Correct 13 ms 768 KB Output is correct
13 Correct 11 ms 768 KB Output is correct
14 Correct 10 ms 768 KB Output is correct
15 Correct 11 ms 768 KB Output is correct
16 Correct 11 ms 768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 2424 KB Output is correct
2 Correct 37 ms 3708 KB Output is correct
3 Correct 40 ms 2688 KB Output is correct
4 Correct 11 ms 768 KB Output is correct
5 Correct 10 ms 768 KB Output is correct
6 Correct 10 ms 640 KB Output is correct
7 Correct 10 ms 640 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 63 ms 3840 KB Output is correct
10 Correct 59 ms 3584 KB Output is correct
11 Correct 45 ms 3708 KB Output is correct
12 Correct 40 ms 3836 KB Output is correct
13 Correct 39 ms 3956 KB Output is correct
14 Correct 40 ms 3956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 640 KB Output is correct
2 Correct 10 ms 640 KB Output is correct
3 Correct 28 ms 2048 KB Output is correct
4 Correct 16 ms 640 KB Output is correct
5 Correct 33 ms 2304 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 81 ms 3324 KB Output is correct
9 Correct 102 ms 3328 KB Output is correct
10 Correct 39 ms 3332 KB Output is correct
11 Correct 36 ms 3192 KB Output is correct
12 Correct 37 ms 3200 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 38 ms 3324 KB Output is correct
20 Correct 42 ms 3332 KB Output is correct
21 Correct 39 ms 3324 KB Output is correct
22 Correct 42 ms 3388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 640 KB Output is correct
2 Correct 10 ms 640 KB Output is correct
3 Correct 10 ms 640 KB Output is correct
4 Correct 12 ms 640 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 10 ms 640 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 15 ms 768 KB Output is correct
11 Correct 27 ms 768 KB Output is correct
12 Correct 13 ms 768 KB Output is correct
13 Correct 11 ms 768 KB Output is correct
14 Correct 10 ms 768 KB Output is correct
15 Correct 11 ms 768 KB Output is correct
16 Correct 11 ms 768 KB Output is correct
17 Correct 34 ms 2424 KB Output is correct
18 Correct 37 ms 3708 KB Output is correct
19 Correct 40 ms 2688 KB Output is correct
20 Correct 11 ms 768 KB Output is correct
21 Correct 10 ms 768 KB Output is correct
22 Correct 10 ms 640 KB Output is correct
23 Correct 10 ms 640 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 63 ms 3840 KB Output is correct
26 Correct 59 ms 3584 KB Output is correct
27 Correct 45 ms 3708 KB Output is correct
28 Correct 40 ms 3836 KB Output is correct
29 Correct 39 ms 3956 KB Output is correct
30 Correct 40 ms 3956 KB Output is correct
31 Correct 10 ms 640 KB Output is correct
32 Correct 10 ms 640 KB Output is correct
33 Correct 28 ms 2048 KB Output is correct
34 Correct 16 ms 640 KB Output is correct
35 Correct 33 ms 2304 KB Output is correct
36 Correct 0 ms 384 KB Output is correct
37 Correct 0 ms 384 KB Output is correct
38 Correct 81 ms 3324 KB Output is correct
39 Correct 102 ms 3328 KB Output is correct
40 Correct 39 ms 3332 KB Output is correct
41 Correct 36 ms 3192 KB Output is correct
42 Correct 37 ms 3200 KB Output is correct
43 Correct 0 ms 384 KB Output is correct
44 Correct 1 ms 384 KB Output is correct
45 Correct 0 ms 384 KB Output is correct
46 Correct 1 ms 384 KB Output is correct
47 Correct 0 ms 384 KB Output is correct
48 Correct 0 ms 384 KB Output is correct
49 Correct 38 ms 3324 KB Output is correct
50 Correct 42 ms 3332 KB Output is correct
51 Correct 39 ms 3324 KB Output is correct
52 Correct 42 ms 3388 KB Output is correct
53 Correct 34 ms 2432 KB Output is correct
54 Correct 34 ms 2424 KB Output is correct
55 Correct 34 ms 2424 KB Output is correct
56 Correct 10 ms 640 KB Output is correct
57 Correct 10 ms 640 KB Output is correct
58 Correct 47 ms 3188 KB Output is correct
59 Correct 47 ms 3188 KB Output is correct
60 Correct 57 ms 2936 KB Output is correct
61 Correct 53 ms 3188 KB Output is correct
62 Correct 336 ms 3188 KB Output is correct
63 Correct 758 ms 2944 KB Output is correct
64 Correct 119 ms 3200 KB Output is correct
65 Correct 323 ms 3208 KB Output is correct
66 Correct 619 ms 3476 KB Output is correct
67 Correct 36 ms 3312 KB Output is correct
68 Correct 62 ms 3580 KB Output is correct
69 Correct 50 ms 3580 KB Output is correct
70 Correct 45 ms 3836 KB Output is correct
71 Correct 47 ms 3836 KB Output is correct
72 Correct 41 ms 3592 KB Output is correct
73 Correct 46 ms 4048 KB Output is correct
74 Correct 54 ms 3836 KB Output is correct