日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

codefores 786B. Legacy(最短路,线段树优化拆点,好题)

發布時間:2023/11/27 生活经验 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 codefores 786B. Legacy(最短路,线段树优化拆点,好题) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接

B. Legacy
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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:

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.

Input
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).

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

題意:給出 \(n\) 個點,存在 \(q\) 條路徑,起點為 \(s\),路徑有一下三種:

1.給出 \(u,v,w\) ,表示點 \(u\) 到點 \(v\) 有一條費用為 \(w\) 的邊.

2.給出 \(v,l,r,w\) 表示點 \(v\) 到區間 \([l,r]\) 的點,費用為 \(w\).

3.給出 \(v,l,r,w\) 表示區間 \([l,r]\) 的點到點 \(v\) ,費用為 \(w\).

求起點 \(s\) 到每個點的最小花費。

題解:其實和這一題思路大體一樣,不過,直接對 \([l,r]\) 縮成點連接 \([l,r]\) 的所有點,那么 \(10^5\)\(q\) 可能得連 \(10^{10}\) 的邊,肯定不行,這里用到了一個輔助工具—線段樹,建立兩顆線段樹,一棵是每個結點連出去的出度“大點”,一棵是連向每個點的入度“大點”,對于第一棵,建樹時就

\(add(seg[i*2][ty],seg[i][ty],0);\)

\(add(seg[i*2+1][ty],seg[i][ty],0);\)

然后對于每個類型2,3,找到對應區間 \([l,r]\) 的點進行連接即可。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<set>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
#define dbg(...) cerr<<"["<<#__VA_ARGS__":"<<(__VA_ARGS__)<<"]"<<endl;
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll inf=1e17;
const ll mod=1000000007;
const int maxn=1e5+100;
int seg[maxn*4][2];
int tot=0;
vector<PII> g[maxn*9];
void add(int u,int v,int w)
{g[u].pb(make_pair(v,w));
}
void build(int i,int l,int r,int ty)
{seg[i][ty]=++tot;if(l==r){if(!ty) add(seg[i][ty],l,0);else add(l,seg[i][ty],0);return;}int m=(l+r)/2;build(i*2,l,m,ty);build(i*2+1,m+1,r,ty);if(!ty){add(seg[i][ty],seg[i*2][ty],0);add(seg[i][ty],seg[i*2+1][ty],0);}else{add(seg[i*2][ty],seg[i][ty],0);add(seg[i*2+1][ty],seg[i][ty],0);}
}VI V;
void update(int i,int l,int r,int L,int R,int ty)
{if(l==L&&r==R){V.pb(seg[i][ty]);return;}int m=(L+R)/2;if(r<=m) update(i*2,l,r,L,m,ty);else if(l>m) update(i*2+1,l,r,m+1,R,ty);else{update(i*2,l,m,L,m,ty);update(i*2+1,m+1,r,m+1,R,ty);}
}
set<pair<ll,int> > st;
ll d[maxn*10];
void dij(int s)
{rep(i,1,tot+1) d[i]=inf;d[s]=0;st.insert(make_pair(0,s));while(!st.empty()){int u=st.begin()->se;st.erase(st.begin());for(int i=0;i<g[u].size();i++){int v=g[u][i].fi;if(d[v]>d[u]+1ll*g[u][i].se){st.erase(make_pair(d[v],v));d[v]=d[u]+1ll*g[u][i].se;st.insert(make_pair(d[v],v));}}}
}
int main()
{int n,q,s;scanf("%d%d%d",&n,&q,&s);tot=n;build(1,1,n,0);build(1,1,n,1);while(q--){int ty,v,u,w;scanf("%d%d",&ty,&v);int l,r;if(ty==1){scanf("%d%d",&u,&w);add(v,u,w);}else{scanf("%d%d%d",&l,&r,&w);V.clear();update(1,l,r,1,n,ty-2);for(int i=0;i<V.size();i++){if(ty-2)add(V[i],v,w);elseadd(v,V[i],w);}}}dij(s);rep(i,1,n+1){if(d[i]==inf) d[i]=-1;printf("%lld ",d[i]);}puts("");return 0;
}

轉載于:https://www.cnblogs.com/tarjan/p/7473525.html

總結

以上是生活随笔為你收集整理的codefores 786B. Legacy(最短路,线段树优化拆点,好题)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。