CodeChef:Two Closest(最短路 & 思维)

来源:互联网 发布:淘宝违法店铺举报 编辑:程序博客网 时间:2024/06/05 17:21

You are given a weighted graph with N nodes and M edges. Some of the nodes are marked as special nodes. Your task is to find the shortest pairwise distance between any two different special nodes.

Input

The first line of the input contains three space-separated integers NM and K denoting the number of nodes, the number of edges, and the number of special nodes.

The following line contains K space-separated distinct integers A1, A2, ..., AK , denoting the special nodes.

Each of the following M lines (say, the jth) contains a triple Xj Yj Zj, denoting the edge connecting the nodes Xj and Yj, and having the weight of Zj.

Output

Output the shortest pairwise distance between any two different special nodes.

Constraints

  • 2 ≤ K ≤ N
  • The given graph is connected.
  • The given graph doesn't contain self loops and multiple edges.
  • 1 ≤ Ai ≤ N
  • 1 ≤ Zj ≤ 104
  • 1 ≤ XjYj ≤ N

Subtasks

  • Subtask #1 (20 points): 2 ≤ N ≤ 300N-1 ≤ M ≤ N*(N-1)/22 ≤ K ≤ N
  • Subtask #2 (25 points): 2 ≤ N ≤ 105N-1 ≤ M ≤ 1052 ≤ K ≤ 10
  • Subtask #3 (55 points): 2 ≤ N ≤ 105N-1 ≤ M ≤ 3 * 1052 ≤ K ≤ 104

Example

Input:5 5 31 3 51 2 32 3 43 4 14 5 81 5 19Output:7

Explanation

Nodes 1, 3 and 5 are special nodes. Shortest distance between nodes 1 and 3 is 7 and that between nodes 3 and 5 is 9. Shortest distance between nodes 1 and 5 is 16. Minimum of these distances is 7. Hence answer is 7.

题意:给一个无向图,K个特殊点,问这K个点中最近的两个点的距离。

思路:特殊点任取一个作为起点跑dijk遇到另外的特殊点就结束,更新最短距离imin,从下一个特殊点开始继续跑,超过imin就break,显然复杂度O(NlogN)。

# include <bits/stdc++.h># define pb push_back# define mp make_pair# define pii pair<int,int># define A first# define B secondusing namespace std;const int maxn = 1e5+10;const int INF = 0x3f3f3f3f;int a[maxn], vis[maxn], dis[maxn];vector<pii >g[maxn];priority_queue<pii, vector<pii >, greater<pii > >q;int main(){    int t, n, m, k, u, v, w, cnt, imin = INF;    scanf("%d%d%d",&n,&m,&k);    for(int i=0; i<k; ++i)    {        scanf("%d",&t);        a[t] = 1;    }    for(int i=0; i<m; ++i)    {        scanf("%d%d%d",&u,&v,&w);        g[u].pb(mp(v,w));        g[v].pb(mp(u,w));    }    for(int i=1; i<=n; ++i)    {        if(!a[i]) continue;        for(int j=1; j<=n; ++j) dis[j] = INF, vis[j] = 0;        cnt = dis[i] = 0;        while(!q.empty()) q.pop();        q.push(mp(0, i));        while(!q.empty())        {            int u = q.top().B;            q.pop();            vis[u] = 1;            cnt += a[u];            if(dis[u] > imin) break;            if(cnt == 2)            {                imin = min(imin, dis[u]);                break;            }            for(auto j : g[u])            {                int v = j.A, w = j.B;                if(vis[v]) continue;                if(dis[v] > dis[u]+w)                {                    dis[v] = dis[u]+w;                    q.push(mp(dis[v], v));                }            }        }    }    printf("%d\n",imin);    return 0;}



原创粉丝点击