Codeforces 196 E. Opening Portals

來源:互聯網
上載者:User

標籤:des   style   color   os   io   for   ar   cti   


Problem E. Opening Portals
首先根據傳送門的性質,如果所有點都是傳送門的話那麼結果就是該圖的最小產生樹。
對於只有其中 k 個結點是傳送門的圖,只要在原演算法的基礎上稍作修改即可。
具體,對每個點求出 P[i] 和 D[i] 。(表示距離這個點最近的傳送門和其距離。。。
之後對每條邊,再根據 D[x] + D[y] + w 作為關鍵字跑最小產生樹。。

以上分別用一次多源 spfa(),和稍作修改的 Kruskal() 即可。


E. Opening Portalstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Pavel plays a famous computer game. A player is responsible for a whole country and he can travel there freely, complete quests and earn experience.

This country has n cities connected by m bidirectional roads of different lengths so that it is possible to get from any city to any other one. There are portals in k of these cities. At the beginning of the game all portals are closed. When a player visits a portal city, the portal opens. Strange as it is, one can teleport from an open portal to an open one. The teleportation takes no time and that enables the player to travel quickly between rather remote regions of the country.

At the beginning of the game Pavel is in city number 1. He wants to open all portals as quickly as possible. How much time will he need for that?

Input

The first line contains two space-separated integers n and m (1?≤?n?≤?105, 0?≤?m?≤?105) that show how many cities and roads are in the game.

Each of the next m lines contains the description of a road as three space-separated integers xi, yi, wi (1?≤?xi,?yi?≤?n, xi?≠?yi, 1?≤?wi?≤?109) — the numbers of the cities connected by the i-th road and the time needed to go from one city to the other one by this road. Any two cities are connected by no more than one road. It is guaranteed that we can get from any city to any other one, moving along the roads of the country.

The next line contains integer k (1?≤?k?≤?n) — the number of portals.

The next line contains k space-separated integers p1, p2, ..., pk — numbers of the cities with installed portals. Each city has no more than one portal.

Output

Print a single number — the minimum time a player needs to open all portals.

Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64dspecifier.

Sample test(s)input
3 31 2 11 3 12 3 131 2 3
output
2
input
4 31 2 12 3 52 4 1032 3 4
output
16
input
4 31 2 10000000002 3 10000000003 4 100000000041 2 3 4
output
3000000000
Note

In the second sample the player has to come to city 2, open a portal there, then go to city 3, open a portal there, teleport back to city 2and finally finish the journey in city 4.





#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;const int maxn=100100;typedef long long int LL;int n,m,c;struct Edge{int from,to,next;LL weight;}edge[3*maxn];int Adj[maxn],Size;int p[maxn],cq[maxn];LL dist[maxn];bool inq[maxn];void init(){memset(Adj,-1,sizeof(Adj)); Size=0;}void Add_Edge(int u,int v,LL w){edge[Size].from=u;edge[Size].to=v;edge[Size].next=Adj[u];edge[Size].weight=w;Adj[u]=Size++;}bool spfa(){memset(dist,63,sizeof(dist));memset(p,0,sizeof(p));memset(cq,0,sizeof(cq));memset(inq,false,sizeof(inq));int k=c;queue<int> q;for(int i=0;i<k;i++){int x;scanf("%d",&x);dist[x]=0; p[x]=x;q.push(x); cq[x]=1; inq[x]=true;}while(!q.empty()){int u=q.front(); q.pop();for(int i=Adj[u];~i;i=edge[i].next){int v=edge[i].to;if(dist[v]>dist[u]+edge[i].weight){dist[v]=dist[u]+edge[i].weight;p[v]=p[u];if(!inq[v]){inq[v]=true;cq[v]++;if(cq[v]>=n) return false;q.push(v);}}}inq[u]=false;}return true;}struct BA{int x,y;LL w;}bian[3*maxn];bool cmp(BA a,BA b){return a.w<b.w;}int fa[maxn];int find(int x){if(fa[x]==x) return x;return fa[x]=find(fa[x]);}int main(){scanf("%d%d",&n,&m);init();for(int i=0;i<m;i++){int a,b,c;scanf("%d%d%d",&a,&b,&c);Add_Edge(a,b,c);Add_Edge(b,a,c);}scanf("%d",&c);spfa();for(int i=0;i<=n+10;i++) fa[i]=i;for(int i=0;i<Size;i++){int u=edge[i].from,v=edge[i].to;bian[i].x=p[u],bian[i].y=p[v];bian[i].w=dist[u]+dist[v]+edge[i].weight;}sort(bian,bian+Size,cmp);int cnt=1;LL ans=dist[1];for(int i=0;i<Size&&cnt<c;i++){int u=bian[i].x,v=bian[i].y;LL w=bian[i].w;if(u==v) continue;int U=find(u),V=find(v);if(U==V) continue;else{ans+=w;fa[U]=V;cnt++;}if(cnt>=c) break;}cout<<ans<<endl;return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.