CF786B Legacy
Rick and his co-workers have made a new radioactive formula and a lot of bad guys are after them. So Rick wants to give his legacy to Morty before bad guys catch them.
There are n planets in their universe numbered from 1 to n. Rick is in planet number s (the earth) and he doesn't know where Morty is. As we all know, Rick owns a portal gun. With this gun he can open one-way portal from a planet he is in to any other planet (including that planet). But there are limits on this gun because he's still using its free trial.
By default he can not open any portal by this gun. There are q plans in the website that sells these guns. Every time you purchase a plan you can only use it once but you can purchase it again if you want to use it more.
Plans on the website have three types:
- With a plan of this type you can open a portal from planet v to planet u.
- With a plan of this type you can open a portal from planet v to any planet with index in range [l, r].
- With a plan of this type you can open a portal from any planet with index in range [l, r] to planet v.
Rick doesn't known where Morty is, but Unity is going to inform him and he wants to be prepared for when he finds and start his journey immediately. So for each planet (including earth itself) he wants to know the minimum amount of money he needs to get from earth to that planet.
The first line of input contains three integers n, q and s (1 ≤ n, q ≤ 105, 1 ≤ s ≤ n) — number of planets, number of plans and index of earth respectively.
The next q lines contain the plans. Each line starts with a number t, type of that plan (1 ≤ t ≤ 3). If t = 1 then it is followed by three integers v, u and w where w is the cost of that plan (1 ≤ v, u ≤ n, 1 ≤ w ≤ 109). Otherwise it is followed by four integers v, l, r and w where w is the cost of that plan (1 ≤ v ≤ n, 1 ≤ l ≤ r ≤ n, 1 ≤ w ≤ 109).
In the first and only line of output print n integers separated by spaces. i-th of them should be minimum money to get from earth to i-th planet, or - 1 if it's impossible to get to that planet.
3 5 1
2 3 2 3 17
2 3 2 2 16
2 2 2 3 3
3 3 1 1 12
1 3 3 17
0 28 12
4 3 1
3 4 1 3 12
2 2 3 4 10
1 2 4 16
0 -1 -1 12
In the first sample testcase, Rick can purchase 4th plan once and then 2nd plan in order to get to get to planet number 2.
1 //2018年2月24日21:30:19 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 #include <queue> 7 using namespace std; 8 9 const int maxn = 5e5+1; 10 typedef long long ll; 11 ll d[maxn<<1], inf; 12 int n, Q, s; 13 vector<int> to[maxn<<1], w[maxn<<1]; 14 15 void addEdge(int x, int y, int z){ 16 to[x].push_back(y); 17 w[x].push_back(z); 18 return; 19 } 20 21 struct node{ 22 int i; 23 long long dis; 24 }; 25 bool operator < (node a, node b){ 26 return a.dis > b.dis; 27 } 28 priority_queue<node> q; 29 30 void buildtree(int k1, int l, int r){ 31 if(l == r){ 32 addEdge(k1+n, l, 0); 33 addEdge(l, k1+maxn+n, 0); 34 return; 35 } 36 int mid = l+r >> 1, lc = k1<<1, rc = lc|1; 37 buildtree(lc, l, mid); 38 buildtree(rc, mid+1, r); 39 addEdge(k1+n, lc+n, 0); addEdge(lc+maxn+n, k1+maxn+n, 0); 40 addEdge(k1+n, rc+n, 0); addEdge(rc+maxn+n, k1+maxn+n, 0); 41 return; 42 } 43 44 int op, x, y, z, v; 45 46 void add(int k1, int l, int r){ 47 if(x<=l && r<=y){ 48 if(op == 2) addEdge(v, k1+n, z); 49 else addEdge(k1+n+maxn, v, z); 50 return; 51 } 52 if(l>y || r<x) return; 53 int mid = l+r >> 1, lc = k1<<1, rc = lc|1; 54 add(lc, l, mid); 55 add(rc, mid+1, r); 56 return; 57 } 58 59 void dijkstra(){ 60 for(int i=1;i<=maxn*2;i++) 61 d[i] = inf; 62 q.push((node){s, 0}); 63 d[s] = 0; 64 while(!q.empty()){ 65 node t = q.top(); 66 x = t.i; 67 q.pop(); 68 if(d[t.i] != t.dis) continue; 69 int siz = to[t.i].size(); 70 for(int i=0; i<siz; i++){ 71 int v = to[x][i]; 72 if(d[v] > d[x] + w[x][i]){ 73 d[v] = d[x] + w[x][i]; 74 q.push((node){v, d[v]}); 75 } 76 } 77 78 } 79 } 80 81 int main(){ 82 inf = 1, inf <<= 60; 83 scanf("%d%d%d", &n, &Q, &s); 84 buildtree(1, 1, n); 85 for(int i=1;i<=Q;i++){ 86 scanf("%d", &op); 87 if(op == 1){ 88 scanf("%d%d%d", &x, &y, &z); 89 addEdge(x, y, z); 90 }else{ 91 scanf("%d%d%d%d", &v, &x, &y, &z); 92 add(1, 1, n); 93 } 94 } 95 dijkstra(); 96 97 for(int i=1;i<=n;i++){ 98 if(d[i] == inf) 99 printf("-1 "); 100 else printf("%I64d ", d[i]); 101 } 102 103 104 return 0; 105 }