Educational Codeforces Round 102 (Rated for Div. 2)E题(分层图、最短路)

https://codeforces.com/contest/1473/problem/E

vector存图:

  1 #define bug(x) cout<<#x<<" is "<<x<<endl
  2 #define IO std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
  3 #include <bits/stdc++.h>
  4 #define iter ::iterator
  5 using namespace  std;
  6 typedef long long ll;
  7 typedef pair<int,ll>P;
  8 #define pb push_back
  9 #define mk make_pair
 10 #define se second
 11 #define fi first
 12 #define rs o*2+1
 13 #define ls o*2
 14 const ll inf=1e18;
 15 const int N=8e5+5;
 16 
 17 int n,m;
 18 
 19 vector<P>g[N];
 20 
 21 void add(int x,int y,ll z){
 22     g[x].pb(mk(y,z));
 23     g[y].pb(mk(x,z));
 24 
 25     g[x+n].pb(mk(y+n,z));
 26     g[y+n].pb(mk(x+n,z));
 27 
 28     g[x+2*n].pb(mk(y+2*n,z));
 29     g[y+2*n].pb(mk(x+2*n,z));
 30 
 31     g[x+3*n].pb(mk(y+3*n,z));
 32     g[y+3*n].pb(mk(x+3*n,z));
 33 
 34     g[x].pb(mk(y+n,0));
 35     g[y].pb(mk(x+n,0));
 36 
 37     g[x+2*n].pb(mk(y+3*n,0));
 38     g[y+2*n].pb(mk(x+3*n,0));
 39 
 40     g[x].pb(mk(y+2*n,2*z));
 41     g[y].pb(mk(x+2*n,2*z));
 42 
 43     g[x+n].pb(mk(y+3*n,2*z));
 44     g[y+n].pb(mk(x+3*n,2*z));
 45 
 46     g[x].pb(mk(y+3*n,z));
 47     g[y].pb(mk(x+3*n,z));
 48 }
 49 
 50 ll dis[N];
 51 
 52 struct node{
 53     int u;
 54     ll w;
 55     bool operator <(const node& t)const{
 56         return w>t.w;
 57     }
 58 };
 59 
 60 void dij(){
 61     priority_queue<node>q;
 62     fill(dis+1,dis+4*n+1,inf);
 63     dis[1]=0;
 64     q.push(node{1,0});
 65     while(!q.empty()){
 66         node now=q.top();
 67         q.pop();
 68         int u=now.u;
 69         if(now.w!=dis[u])continue;//优化,如果已经更新了与u相连的点通过u点到起点的距离那么无需再更新
 70         for(int i=0;i<g[u].size();i++){
 71             int v=g[u][i].fi;
 72             ll w=g[u][i].se;
 73             //bug(w);a
 74             if(dis[u]+w<dis[v]){
 75                 dis[v]=dis[u]+w;
 76                 q.push(node{v,dis[v]});
 77             }
 78         }
 79     }
 80 
 81 }
 82 
 83 struct node1{
 84     int x,y;
 85     ll z;
 86 }e[N];
 87 
 88 int main(){
 89     IO;
 90     cin>>n>>m;
 91     for(int i=1;i<=m;i++){
 92         int u,v;
 93         ll w;
 94         cin>>u>>v>>w;
 95         add(u,v,w);
 96     }
 97     dij();
 98     for(int i=2;i<=n;i++){
 99         //printf("%lld\n",dis[i+3*n]);
100         cout<<dis[i+3*n]<<" ";
101     }
102     cout<<endl;
103 }

 前向星存图

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef long long LL;
 5 const LL INF = 1e18;
 6 const int N = 2e5 + 20;
 7 
 8 struct Edge
 9 {
10     int to, nxt, w;
11 }line[N * 20];
12 int fist[N * 4], idx;
13 int n, m;
14 
15 void add(int x, int y, int z)
16 {
17     line[idx] = (Edge){y, fist[x], z};
18     fist[x] = idx ++;
19 }
20 
21 void addedge(int x, int y, int z)
22 {
23     add(x, y, z); 
24 
25     add(x + n, y + n, z);
26 
27     add(x + 2 * n, y + 2 * n, z); 
28     
29     dd(x + 3 * n, y + 3 * n, z);
30 
31     add(x, y + n, 0); 
32 
33     add(x + 2 * n, y + 3 * n, 0);
34 
35     add(x, y + 2 * n, 2 * z); 
36 
37     add(x + n, y + 3 * n, 2 * z);
38 
39     add(x, y + 3 * n, z);
40 }
41 
42 bool st[N * 4];
43 LL dis[N * 4];
44 struct zt
45 {
46     int x;
47     LL d;
48 };
49 bool operator < (zt a, zt b)
50 {
51     return a.d > b.d;
52 }
53 
54 void heap_dijkstra()
55 {
56     priority_queue<zt> q;
57     for(int i = 1; i <= 4 * n; ++ i) dis[i] = INF;
58     dis[1] = 0;
59     q.push((zt){1, 0});
60     while(!q.empty())
61     {
62         zt u = q.top(); q.pop();
63         if(st[u.x]) continue;
64         st[u.x] = 1;
65         for(int i = fist[u.x]; i != -1; i = line[i].nxt)
66         {
67             int v = line[i].to;
68             if(dis[v] > dis[u.x] + line[i].w)
69             {
70                 dis[v] = dis[u.x] + line[i].w;
71                 q.push((zt){v, dis[v]});
72             }
73         }
74     }
75 }
76 
77 int main()
78 {
79     // freopen("E.in", "r", stdin);
80     memset(fist, -1, sizeof fist);
81     scanf("%d%d", &n, &m);
82     for(int i = 1; i <= m; ++ i)
83     {
84         int a, b, c;
85         scanf("%d%d%d", &a, &b, &c);
86         addedge(a, b, c);
87         addedge(b, a, c);
88     }
89     heap_dijkstra();
90     for(int i = 2; i <= n; ++ i)
91         printf("%lld\n", dis[i]);
92     //puts("");
93     return 0;
94 }

 

上一篇:「ROI 2018 Day 1」量子隐形传态


下一篇:AcWing 3034. 望远镜 POJ3675 三角剖分求圆与多边形的面积交并