Legacy

Legacy

http://codeforces.com/problemset/problem/787/D

 

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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:

  1. With a plan of this type you can open a portal from planet v to planet u.
  2. With a plan of this type you can open a portal from planet v to any planet with index in range [l, r].
  3. 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.

Input

The first line of input contains three integers nq 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 vu and w where w is the cost of that plan (1 ≤ v, u ≤ n1 ≤ w ≤ 109). Otherwise it is followed by four integers vlr and w where w is the cost of that plan (1 ≤ v ≤ n1 ≤ l ≤ r ≤ n1 ≤ w ≤ 109).

Output

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.

Examples
input
Copy
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
output
Copy
0 28 12 
input
Copy
4 3 1
3 4 1 3 12
2 2 3 4 10
1 2 4 16
output
Copy
0 -1 -1 12 
Note

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 #include<bits/stdc++.h>
  2 typedef long long ll;
  3 #define lson l,mid,rt<<1
  4 #define rson mid+1,r,rt<<1|1
  5 #define pb push_back
  6 #define maxn 100005
  7 using namespace std;
  8 
  9 int id[maxn<<2][2];
 10 int idx;
 11 ll dis[maxn*5];
 12 int vis[maxn*5];
 13 int n,q,s;
 14 vector<pair<ll,int> >ve[maxn*5];
 15 vector<int>V;
 16 
 17 void build(int l,int r,int rt,int p){
 18     id[rt][p]=++idx;
 19     if(l==r){
 20         if(!p){
 21             ve[idx].pb({0,l});
 22         }
 23         else{
 24             ve[l].pb({0,idx});
 25         }
 26         return;
 27     }
 28     int mid=l+r>>1;
 29     build(lson,p);
 30     build(rson,p);
 31     if(!p){
 32         ve[id[rt][p]].pb({0,id[rt<<1][p]});
 33         ve[id[rt][p]].pb({0,id[rt<<1|1][p]});
 34     }
 35     else{
 36         ve[id[rt<<1][p]].pb({0,id[rt][p]});
 37         ve[id[rt<<1|1][p]].pb({0,id[rt][p]});
 38     }
 39 }
 40 
 41 void query(int L,int R,int l,int r,int rt,int p){
 42     if(L<=l&&R>=r){
 43         V.pb(id[rt][p]);
 44         return;
 45     }
 46     int mid=l+r>>1;
 47     if(L<=mid) query(L,R,lson,p);
 48     if(R>mid) query(L,R,rson,p);
 49 }
 50 
 51 void Dijstra(){
 52     priority_queue<pair<ll,int> >Q;
 53     Q.push({0,s});
 54     for(int i=0;i<maxn*5;i++){
 55         dis[i]=0x3f3f3f3f3f3f3f3f;
 56         vis[i]=0;
 57     }
 58     dis[s]=0;
 59     pair<ll,int>pp;
 60     while(!Q.empty()){
 61         pp=Q.top();
 62         Q.pop();
 63         if(!vis[pp.second]){
 64             vis[pp.second]=1;
 65             for(int i=0;i<ve[pp.second].size();i++){
 66                 int nex=ve[pp.second][i].second;;
 67                 ll len=ve[pp.second][i].first;
 68                 if(dis[nex]>dis[pp.second]+len){
 69                     dis[nex]=dis[pp.second]+len;
 70                     Q.push({-dis[nex],nex});
 71                 }
 72             }
 73         }
 74     }
 75     for(int i=1;i<=n;i++){
 76         if(dis[i]==0x3f3f3f3f3f3f3f3f){
 77             dis[i]=-1;
 78         }
 79         cout<<dis[i]<<" ";
 80     }
 81 }
 82 
 83 int main(){
 84     std::ios::sync_with_stdio(false);
 85     cin>>n>>q>>s;
 86     idx=n;
 87     build(1,n,1,0);
 88     build(1,n,1,1);
 89     int opt,l,r,v,u;
 90     ll w;
 91     for(int i=1;i<=q;i++){
 92         cin>>opt;
 93         if(opt==1){
 94             cin>>v>>u>>w;
 95             ve[v].pb({w,u});
 96         }
 97         else if(opt==2){
 98             V.clear();
 99             cin>>v>>l>>r>>w;
100             query(l,r,1,n,1,0);
101             for(int i=0;i<V.size();i++){
102                 ve[v].pb({w,V[i]});
103             }
104         }
105         else{
106             V.clear();
107             cin>>v>>l>>r>>w;
108             query(l,r,1,n,1,1);
109             for(int i=0;i<V.size();i++){
110                 ve[V[i]].pb({w,v});
111             }
112         }
113     }
114     Dijstra();
115 
116 }
View Code

 

posted on 2019-05-13 20:12  Fighting_sh  阅读(220)  评论(0编辑  收藏  举报

导航