日韩性视频-久久久蜜桃-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(最短路,线段树优化拆点,好题)的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 欧美一区二区视频在线 | 久久久久久久久久久久91 | 国产肉体xxxx裸体784大胆 | 依依成人综合网 | 精品看片 | 久久网伊人 | 亚洲在线观看视频 | 天堂av免费在线观看 | 1级片在线观看 | 99热这里只有精品99 | 亚洲30p | 国产一区二区三区四区精 | 久草视频网站 | 国产99久久久欧美黑人 | 欧美日韩国产大片 | 77久久 | 女人叫床高潮娇喘声录音mp3 | 国产精品主播一区二区 | 中国美女一级片 | 日本囗交做爰视频 | 日本美女视频一区 | 天堂伊人网 | 日韩欧美成人免费视频 | 性生交生活影碟片 | 四虎国产视频 | 日韩最新视频 | 毛片基地免费观看 | 涩av | 日韩中字在线 | 无码人妻精品一区二区三区9厂 | 在线se | 黄色链接视频 | 亚洲24p | av毛片大全| 久免费一级suv好看的国产 | 牛牛av| 日韩网站免费观看 | av福利社| 亚洲高潮无码久久 | 人人超碰在线 | 国产精品久久久久久 | 久久嫩草| 女人脱了内裤趴开腿让男躁 | 色xxxxxx | 国产又大又黑又粗 | 中国国产bdsm紧缚捆绑 | 亚色视频在线观看 | 中文在线一区二区三区 | 国产精品羞羞答答 | 日韩一区二区高清视频 | 久久久激情网 | 国内三级视频 | 欧美一级日韩 | av无码久久久久久不卡网站 | 中文字幕成人在线 | 精品久久一 | 一级片自拍 | 年下总裁被打光屁股sp | 老司机精品视频网站 | 亚洲天堂91 | 国产精品一二三区 | 日本精品一区二区 | 国产精品一区二区三区免费观看 | 欧美另类z0z变态 | 久久大陆 | 91精品国产综合久久久蜜臀图片 | 久草美女 | 永久免费看mv网站入口78 | 国产吃瓜黑料一区二区 | 激情自拍偷拍 | 婷婷六月天 | 国产无遮挡裸体免费视频 | 天天射视频 | 在线视频这里只有精品 | 国产99自拍| 壮汉被书生c到合不拢腿 | av天天色 | 在线观看av的网址 | 日操干| 国产a级片视频 | 男插女青青影院 | 国产精品一区二区三区四区五区 | 亚洲成色www久久网站 | 成年人午夜影院 | 欧美日韩国产精品一区二区三区 | 美女av免费 | 少妇裸体性生交 | 在线看片| 欧美日韩不卡一区二区 | 成人激情开心 | 国产香蕉视频在线播放 | 韩国一二三区 | a级片中文字幕 | 婷婷在线看 | 青青青国产在线 | 国产成人在线免费观看 | 亚洲av女人18毛片水真多 | av福利在线播放 | 亚洲国产日韩一区无码精品久久久 |