ABC463E Dijkstra+超级源点
::::info[毫无意义的背景] 由于是插班进的,最短路只学了模版,然后被这么套路的题卡了…… :::: ::::info[传送门] Atcoder,洛谷。 :::: ::::info[方案一] 这个题要最短路,显然这个没有问题。但是这边有个传送门的奇怪设计。这怎么办呢?
| ::::info[方案二] |
这个题就是P2294 [HNOI2005] 狡猾的商人,没错,仅仅两个月前。
和那道题一样,这个题也要用一个东西,叫做“超级源点”,是一个虚拟节点,你可以把它的编号设为
看到题目,传送门的代价
| 然后再进行 Dijkstra 就行了。 |
|---|
| ::::info[实现] |
| 请使用堆优化 Dijkstra,不然这个题你将会 TLE 得很惨。 |
| 对于 Dijkstra 有问题的,请走步P4779 【模板】单源最短路径(标准版)。 |
|---|
| ::::error[我没敢写,但也没想到真会 TLE 的Code] |
| ```cpp |
| #define LL long long |
| #define F(i,k,n) for(int i=(k);i<=(n);++i) |
| #define RF(i,k,n) for(int i=(k);i>=(n);--i) |
| using namespace std; |
| struct node{ |
| int u; |
| int v; |
| int t; |
| }rd[200005]; |
| int f[200005]; |
| LL mt[200005]; |
| vector<pair<int,int>>mp[200005]; |
| signed main(){ |
| ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); |
| int n,m,y; |
| cin>>n>>m>>y; |
| F(i,1,m){ |
| int u,v,t; |
| cin>>u>>v>>t; |
| mp[u].push_back({v,t}); |
| mp[v].push_back({u,t}); |
| } |
| F(i,1,n){ |
| cin>>f[i]; |
| mp[i].push_back({0,f[i]}); |
| mp[0].push_back({i,y+f[i]}); |
| mt[i]=1e18; |
| } |
| mt[0]=1e18; |
| mt[1]=0; |
| priority_queue<pair<LL,int>, |
| vector<pair<LL,int>>,greater<pair<LL,int>>>hq; |
| hq.push({0,1}); |
| while(!hq.empty()){ |
| pair<LL,int>nw=hq.top(); |
| hq.pop(); |
| for(auto it:mp[nw.second]){ |
| LL ths=nw.first+it.second; |
| if(ths<mt[it.first]){ |
| mt[it.first]=ths; |
| hq.push({ths,it.first}); |
| } |
| } |
| } |
| F(i,2,n){ |
| cout<<mt[i]<<' '; |
| } |
| return 0; |
| } |
| ``` |
| 评测记录。 |
这个问题,其实我在赛中没有想到虚拟节点的时候,就已经想到了,但我没想到,数据真的卡了这个。
| 请大家自行思考为什么会 TLE。 |
|---|
| ::::info[优化] |
| 观察这段代码 |
| ```cpp |
| while(!hq.empty()){ |
| pair<LL,int>nw=hq.top(); |
| hq.pop(); |
| for(auto it:mp[nw.second]){ |
| LL ths=nw.first+it.second; |
| if(ths<mt[it.first]){ |
| mt[it.first]=ths; |
| hq.push({ths,it.first}); |
| } |
| } |
| } |
| ``` |
| 发现一个节点会反复入队,所以我们要做一个标记。 |
| :::: |
| ::::success[AC Code] |
| ```cpp |
| #define LL long long |
| #define F(i,k,n) for(int i=(k);i<=(n);++i) |
| #define RF(i,k,n) for(int i=(k);i>=(n);--i) |
| using namespace std; |
| struct node{ |
| int u; |
| int v; |
| int t; |
| }rd[200005]; |
| int f[200005]; |
| LL mt[200005]; |
| bool b[200005]; |
| vector<pair<int,int>>mp[200005]; |
| signed main(){ |
| ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); |
| int n,m,y; |
| cin>>n>>m>>y; |
| F(i,1,m){ |
| int u,v,t; |
| cin>>u>>v>>t; |
| mp[u].push_back({v,t}); |
| mp[v].push_back({u,t}); |
| } |
| F(i,1,n){ |
| cin>>f[i]; |
| mp[i].push_back({0,f[i]}); |
| mp[0].push_back({i,y+f[i]}); |
| mt[i]=1e18; |
| } |
| mt[0]=1e18; |
| mt[1]=0; |
| priority_queue<pair<LL,int>, |
| vector<pair<LL,int>>,greater<pair<LL,int>>>hq; |
| hq.push({0,1}); |
| b[1]=1; |
| while(!hq.empty()){ |
| pair<LL,int>nw=hq.top(); |
| hq.pop(); |
| if(b[nw.second]==0) |
| continue; |
| b[nw.second]=0; |
| for(auto it:mp[nw.second]){ |
| LL ths=nw.first+it.second; |
| if(ths<mt[it.first]){ |
| mt[it.first]=ths; |
| hq.push({ths,it.first}); |
| b[it.first]=1; |
| } |
| } |
| } |
| F(i,2,n){ |
| cout<<mt[i]<<' '; |
| } |
| return 0; |
| } |
| ``` |
| 评测记录。 |
| :::: |