답안 #380979

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
380979 2021-03-24T01:13:45 Z PedroBigMan Relay Marathon (NOI20_relaymarathon) C++14
100 / 100
2625 ms 370884 KB
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <deque>
#include <list>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
#define REP(i,a,b) for(ll i=a; i<b; i++)
#define pb push_back
#define mp make_pair
#define pl pair<ll,ll>
#define ff first
#define ss second
#define whole(x) x.begin(),x.end()
#define DEBUG(i) cout<<"Pedro Is The Master "<<i<<endl
#define INF 100000000000000000LL
ll insig;
#define In(vecBRO, LENBRO) REP(IBRO,0,LENBRO) {cin>>insig; vecBRO.pb(insig);}
void Out(vector<ll> x) {REP(i,0,x.size()) {cout<<x[i]<<" ";} cout<<endl;}

class WG //everything works for weighted directed graphs
{
    public:
    ll N; vector<vector<pl> > adj;
    vector<bool> pr; vector<ll> nv;
    
    WG(vector<vector<pl> > ad)
    {
        adj=ad; N=adj.size();
        REP(i,0,N) {pr.pb(false); nv.pb(0);}
    }
    
    void Reset()
    {
        REP(i,0,N) {pr[i]=false;nv.pb(0);}
    }
    
    vector<ll> Djikstra(ll s)
    {
        Reset();
        vector<ll> d; REP(i,0,N) {d.pb(INF);}
        d[s]=0;
        priority_queue<pl> q;
        q.push(mp(0,s));
        ll cur;
        while(!q.empty())
        {
            cur=q.top().ss; q.pop();
            if(pr[cur]) {continue;}
            pr[cur]=true; 
            REP(i,0,adj[cur].size())
            {
                if(d[adj[cur][i].ff]>d[cur]+adj[cur][i].ss)
                {
                    d[adj[cur][i].ff]=d[cur]+adj[cur][i].ss;
                    q.push(mp(-d[adj[cur][i].ff],adj[cur][i].ff));
                }
            }
        }
        return d;
    }
    
    vector<pl> Djikstra_MS(vector<ll> sn) //Djikstra Multi-sourced, ans[i].ff=d(i,sn), ans[i].ss=member of sn closest to i
    {
        Reset();
        ll K=sn.size();
        vector<pl> d; REP(i,0,N) {d.pb(mp(INF,-1LL));}
        REP(i,0,K) {d[sn[i]]=mp(0LL,sn[i]);}
        priority_queue<pl> q;
        REP(i,0,K) {q.push(mp(0,sn[i]));}
        ll cur;
        while(!q.empty())
        {
            cur=q.top().ss; q.pop();
            if(pr[cur]) {continue;}
            pr[cur]=true; 
            REP(i,0,adj[cur].size())
            {
                if(d[adj[cur][i].ff].ff>d[cur].ff+adj[cur][i].ss)
                {
                    d[adj[cur][i].ff].ff=d[cur].ff+adj[cur][i].ss;
                    d[adj[cur][i].ff].ss=d[cur].ss;
                    q.push(mp(-d[adj[cur][i].ff].ff,adj[cur][i].ff));
                }
            }
        }
        return d;
    }
};

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    ll N,M,K; cin>>N>>M>>K; vector<ll> A; vector<pl> xx; vector<vector<pl> > adj; REP(i,0,N) {adj.pb(xx);}
    pl cur; ll we;
    REP(i,0,M) {cin>>cur.ff>>cur.ss>>we; cur.ff--; cur.ss--; adj[cur.ff].pb(mp(cur.ss,we)); adj[cur.ss].pb(mp(cur.ff,we));}
    WG G(adj);
    In(A,K); REP(i,0,K) {A[i]--;}
    ll ans=INF; pl co; ll sp=INF;
    vector<pl> d=G.Djikstra_MS(A);
    REP(i,0,N)
    {
        REP(j,0,adj[i].size())
        {
            if(d[i].ss==d[adj[i][j].ff].ss) {continue;}
            if(d[i].ff+d[adj[i][j].ff].ff+adj[i][j].ss<sp)
            {
                sp=d[i].ff+d[adj[i][j].ff].ff+adj[i][j].ss;
                co=mp(d[i].ss,d[adj[i][j].ff].ss);
            }
        }
    }
    ans=sp;
    sp=INF;
    pl co2;
    A.erase(find(whole(A),co.ff));
    A.erase(find(whole(A),co.ss));
    d=G.Djikstra_MS(A);
    REP(i,0,N)
    {
        REP(j,0,adj[i].size())
        {
            if(i==co.ff || i==co.ss || adj[i][j].ff==co.ff || adj[i][j].ff==co.ss) {continue;}
            if(d[i].ss==d[adj[i][j].ff].ss) {continue;}
            if(d[i].ff+d[adj[i][j].ff].ff+adj[i][j].ss<sp)
            {
                sp=d[i].ff+d[adj[i][j].ff].ff+adj[i][j].ss;
                co2=mp(d[i].ss,d[adj[i][j].ff].ss);
            }
        }
    }
    ans+=sp;
    
    vector<ll> d1=G.Djikstra(co.ff); 
    vector<ll> d2=G.Djikstra(co.ss);
    
    ll mindist1=INF; ll par1;  ll mindist2=INF; ll par2;
    REP(j,0,K) 
    {
        ll i=A[j];
        if(i==co.ff || i==co.ss) {continue;}
        if(d1[i]<mindist1) {mindist1=d1[i]; par1=i;}
    }
    REP(j,0,K) 
    {
        ll i=A[j];
        if(i==co.ff || i==co.ss || i==par1) {continue;}
        if(d2[i]<mindist2) {mindist2=d2[i]; par2=i;}
    }
    ans=min(ans,mindist1+mindist2);
    
    mindist1=INF; mindist2=INF;
    REP(j,0,K) 
    {
        ll i=A[j];
        if(i==co.ff || i==co.ss) {continue;}
        if(d2[i]<mindist2) {mindist2=d2[i]; par2=i;}
    }
    REP(j,0,K) 
    {
        ll i=A[j];
        if(i==co.ff || i==co.ss || i==par2) {continue;}
        if(d1[i]<mindist1) {mindist1=d1[i]; par1=i;}
    }
    ans=min(ans,mindist1+mindist2);
    cout<<ans<<endl;
    return 0;
}

Compilation message

RelayMarathon.cpp: In function 'void Out(std::vector<long long int>)':
RelayMarathon.cpp:17:33: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   17 | #define REP(i,a,b) for(ll i=a; i<b; i++)
......
   28 | void Out(vector<ll> x) {REP(i,0,x.size()) {cout<<x[i]<<" ";} cout<<endl;}
      |                             ~~~~~~~~~~~~
RelayMarathon.cpp:28:25: note: in expansion of macro 'REP'
   28 | void Out(vector<ll> x) {REP(i,0,x.size()) {cout<<x[i]<<" ";} cout<<endl;}
      |                         ^~~
RelayMarathon.cpp: In member function 'std::vector<long long int> WG::Djikstra(ll)':
RelayMarathon.cpp:17:33: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   17 | #define REP(i,a,b) for(ll i=a; i<b; i++)
......
   60 |             REP(i,0,adj[cur].size())
      |                 ~~~~~~~~~~~~~~~~~~~
RelayMarathon.cpp:60:13: note: in expansion of macro 'REP'
   60 |             REP(i,0,adj[cur].size())
      |             ^~~
RelayMarathon.cpp: In member function 'std::vector<std::pair<long long int, long long int> > WG::Djikstra_MS(std::vector<long long int>)':
RelayMarathon.cpp:17:33: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   17 | #define REP(i,a,b) for(ll i=a; i<b; i++)
......
   86 |             REP(i,0,adj[cur].size())
      |                 ~~~~~~~~~~~~~~~~~~~
RelayMarathon.cpp:86:13: note: in expansion of macro 'REP'
   86 |             REP(i,0,adj[cur].size())
      |             ^~~
RelayMarathon.cpp: In function 'int main()':
RelayMarathon.cpp:17:33: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   17 | #define REP(i,a,b) for(ll i=a; i<b; i++)
......
  113 |         REP(j,0,adj[i].size())
      |             ~~~~~~~~~~~~~~~~~    
RelayMarathon.cpp:113:9: note: in expansion of macro 'REP'
  113 |         REP(j,0,adj[i].size())
      |         ^~~
RelayMarathon.cpp:17:33: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   17 | #define REP(i,a,b) for(ll i=a; i<b; i++)
......
  131 |         REP(j,0,adj[i].size())
      |             ~~~~~~~~~~~~~~~~~    
RelayMarathon.cpp:131:9: note: in expansion of macro 'REP'
  131 |         REP(j,0,adj[i].size())
      |         ^~~
RelayMarathon.cpp:172:33: warning: 'par2' may be used uninitialized in this function [-Wmaybe-uninitialized]
  172 |         if(i==co.ff || i==co.ss || i==par2) {continue;}
      |            ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
RelayMarathon.cpp:157:33: warning: 'par1' may be used uninitialized in this function [-Wmaybe-uninitialized]
  157 |         if(i==co.ff || i==co.ss || i==par1) {continue;}
      |            ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 1 ms 492 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 492 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 492 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 492 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 492 KB Output is correct
29 Correct 1 ms 492 KB Output is correct
30 Correct 1 ms 492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 1 ms 492 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 492 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 492 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 492 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 492 KB Output is correct
29 Correct 1 ms 492 KB Output is correct
30 Correct 1 ms 492 KB Output is correct
31 Correct 1 ms 492 KB Output is correct
32 Correct 1 ms 492 KB Output is correct
33 Correct 1 ms 492 KB Output is correct
34 Correct 1 ms 492 KB Output is correct
35 Correct 1 ms 492 KB Output is correct
36 Correct 4 ms 876 KB Output is correct
37 Correct 4 ms 1132 KB Output is correct
38 Correct 1 ms 492 KB Output is correct
39 Correct 31 ms 10988 KB Output is correct
40 Correct 8 ms 2668 KB Output is correct
41 Correct 2 ms 492 KB Output is correct
42 Correct 8 ms 2668 KB Output is correct
43 Correct 3 ms 748 KB Output is correct
44 Correct 2 ms 492 KB Output is correct
45 Correct 1 ms 492 KB Output is correct
46 Correct 37 ms 12780 KB Output is correct
47 Correct 6 ms 2028 KB Output is correct
48 Correct 28 ms 10476 KB Output is correct
49 Correct 32 ms 11244 KB Output is correct
50 Correct 1 ms 492 KB Output is correct
51 Correct 1 ms 492 KB Output is correct
52 Correct 1 ms 492 KB Output is correct
53 Correct 18 ms 6124 KB Output is correct
54 Correct 33 ms 11500 KB Output is correct
55 Correct 1 ms 492 KB Output is correct
56 Correct 1 ms 492 KB Output is correct
57 Correct 1 ms 492 KB Output is correct
58 Correct 41 ms 13548 KB Output is correct
59 Correct 1 ms 492 KB Output is correct
60 Correct 2 ms 620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 111 ms 26164 KB Output is correct
2 Correct 25 ms 16180 KB Output is correct
3 Correct 1887 ms 285652 KB Output is correct
4 Correct 923 ms 131340 KB Output is correct
5 Correct 280 ms 37924 KB Output is correct
6 Correct 222 ms 32820 KB Output is correct
7 Correct 302 ms 40500 KB Output is correct
8 Correct 127 ms 26548 KB Output is correct
9 Correct 213 ms 30260 KB Output is correct
10 Correct 158 ms 28852 KB Output is correct
11 Correct 1973 ms 298828 KB Output is correct
12 Correct 166 ms 29748 KB Output is correct
13 Correct 590 ms 88116 KB Output is correct
14 Correct 307 ms 42164 KB Output is correct
15 Correct 1955 ms 293340 KB Output is correct
16 Correct 93 ms 24884 KB Output is correct
17 Correct 1365 ms 193044 KB Output is correct
18 Correct 27 ms 16308 KB Output is correct
19 Correct 2079 ms 304348 KB Output is correct
20 Correct 293 ms 40116 KB Output is correct
21 Correct 272 ms 37572 KB Output is correct
22 Correct 145 ms 28084 KB Output is correct
23 Correct 58 ms 22196 KB Output is correct
24 Correct 1377 ms 215644 KB Output is correct
25 Correct 208 ms 33080 KB Output is correct
26 Correct 118 ms 26420 KB Output is correct
27 Correct 176 ms 30516 KB Output is correct
28 Correct 44 ms 19124 KB Output is correct
29 Correct 300 ms 39732 KB Output is correct
30 Correct 521 ms 68148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 1 ms 492 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 492 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 492 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 492 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 492 KB Output is correct
29 Correct 1 ms 492 KB Output is correct
30 Correct 1 ms 492 KB Output is correct
31 Correct 1 ms 492 KB Output is correct
32 Correct 1 ms 492 KB Output is correct
33 Correct 1 ms 492 KB Output is correct
34 Correct 1 ms 492 KB Output is correct
35 Correct 1 ms 492 KB Output is correct
36 Correct 4 ms 876 KB Output is correct
37 Correct 4 ms 1132 KB Output is correct
38 Correct 1 ms 492 KB Output is correct
39 Correct 31 ms 10988 KB Output is correct
40 Correct 8 ms 2668 KB Output is correct
41 Correct 2 ms 492 KB Output is correct
42 Correct 8 ms 2668 KB Output is correct
43 Correct 3 ms 748 KB Output is correct
44 Correct 2 ms 492 KB Output is correct
45 Correct 1 ms 492 KB Output is correct
46 Correct 37 ms 12780 KB Output is correct
47 Correct 6 ms 2028 KB Output is correct
48 Correct 28 ms 10476 KB Output is correct
49 Correct 32 ms 11244 KB Output is correct
50 Correct 1 ms 492 KB Output is correct
51 Correct 1 ms 492 KB Output is correct
52 Correct 1 ms 492 KB Output is correct
53 Correct 18 ms 6124 KB Output is correct
54 Correct 33 ms 11500 KB Output is correct
55 Correct 1 ms 492 KB Output is correct
56 Correct 1 ms 492 KB Output is correct
57 Correct 1 ms 492 KB Output is correct
58 Correct 41 ms 13548 KB Output is correct
59 Correct 1 ms 492 KB Output is correct
60 Correct 2 ms 620 KB Output is correct
61 Correct 111 ms 26164 KB Output is correct
62 Correct 25 ms 16180 KB Output is correct
63 Correct 1887 ms 285652 KB Output is correct
64 Correct 923 ms 131340 KB Output is correct
65 Correct 280 ms 37924 KB Output is correct
66 Correct 222 ms 32820 KB Output is correct
67 Correct 302 ms 40500 KB Output is correct
68 Correct 127 ms 26548 KB Output is correct
69 Correct 213 ms 30260 KB Output is correct
70 Correct 158 ms 28852 KB Output is correct
71 Correct 1973 ms 298828 KB Output is correct
72 Correct 166 ms 29748 KB Output is correct
73 Correct 590 ms 88116 KB Output is correct
74 Correct 307 ms 42164 KB Output is correct
75 Correct 1955 ms 293340 KB Output is correct
76 Correct 93 ms 24884 KB Output is correct
77 Correct 1365 ms 193044 KB Output is correct
78 Correct 27 ms 16308 KB Output is correct
79 Correct 2079 ms 304348 KB Output is correct
80 Correct 293 ms 40116 KB Output is correct
81 Correct 272 ms 37572 KB Output is correct
82 Correct 145 ms 28084 KB Output is correct
83 Correct 58 ms 22196 KB Output is correct
84 Correct 1377 ms 215644 KB Output is correct
85 Correct 208 ms 33080 KB Output is correct
86 Correct 118 ms 26420 KB Output is correct
87 Correct 176 ms 30516 KB Output is correct
88 Correct 44 ms 19124 KB Output is correct
89 Correct 300 ms 39732 KB Output is correct
90 Correct 521 ms 68148 KB Output is correct
91 Correct 257 ms 28724 KB Output is correct
92 Correct 2469 ms 348212 KB Output is correct
93 Correct 420 ms 42440 KB Output is correct
94 Correct 1576 ms 195548 KB Output is correct
95 Correct 28 ms 16308 KB Output is correct
96 Correct 29 ms 16692 KB Output is correct
97 Correct 505 ms 47940 KB Output is correct
98 Correct 263 ms 31796 KB Output is correct
99 Correct 214 ms 33844 KB Output is correct
100 Correct 2608 ms 368368 KB Output is correct
101 Correct 1159 ms 132856 KB Output is correct
102 Correct 1141 ms 128540 KB Output is correct
103 Correct 1715 ms 243164 KB Output is correct
104 Correct 2148 ms 287152 KB Output is correct
105 Correct 171 ms 26932 KB Output is correct
106 Correct 1773 ms 245804 KB Output is correct
107 Correct 705 ms 71476 KB Output is correct
108 Correct 384 ms 39348 KB Output is correct
109 Correct 46 ms 20532 KB Output is correct
110 Correct 135 ms 25524 KB Output is correct
111 Correct 384 ms 38716 KB Output is correct
112 Correct 2583 ms 368480 KB Output is correct
113 Correct 2625 ms 370884 KB Output is correct
114 Correct 384 ms 39092 KB Output is correct
115 Correct 992 ms 121656 KB Output is correct
116 Correct 2405 ms 342028 KB Output is correct
117 Correct 1722 ms 237436 KB Output is correct
118 Correct 1583 ms 211152 KB Output is correct
119 Correct 2236 ms 305320 KB Output is correct
120 Correct 1484 ms 175824 KB Output is correct