2017 Multi-University Training Contest 9 1006 Senior Pan spfa

来源:互联网 发布:java dll 反编译工具 编辑:程序博客网 时间:2024/06/13 17:37

Senior Pan

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0


Problem Description
Senior Pan fails in his discrete math exam again. So he asks Master ZKC to give him graph theory problems everyday.
The task is simple : ZKC will give Pan a directed graph every time, and selects some nodes from that graph, you can calculate the minimum distance of every pair of nodes chosen in these nodes and now ZKC only cares about the minimum among them. That is still too hard for poor Pan, so he asks you for help.
 

Input
The first line contains one integer T, represents the number of Test Cases.1≤T≤5.Then T Test Cases, for each Test Cases, the first line contains two integers n,m representing the number of nodes and the number of edges.1≤n,m≤100000
Then m lines follow. Each line contains three integers xi,yi representing an edge, and vi representing its length.1≤xi,yi≤n,1≤vi≤100000
Then one line contains one integer K, the number of nodes that Master Dong selects out.1≤K≤n
The following line contains K unique integers ai, the nodes that Master Dong selects out.1≤ai≤n,ai!=aj
 

Output
For every Test Case, output one integer: the answer
 

Sample Input
15 61 2 12 3 33 1 32 5 12 4 24 3 131 3 5
 

Sample Output
Case #1: 2
k个点分别按顺序和倒序加入队列,每次加一个点就以当前点为七点跑一次spfa(),仔细想想复杂度不是很高,因为spfa的复杂度取决于
dis数组的更新频繁与否,保留dis数组就不会超时,那么只要在spfa过程中更新答案就行了。
#include<stdio.h>#include<algorithm>#include<string.h>#include<vector>#include<queue>using namespace std;const int maxm = 100005;const int INF = 1e9 + 7;struct node{    int v, w;};vector<node>p[maxm];int flag[maxm], a[maxm];long long dis[maxm], ans = 1e15;void bfs(int s);int main() {    int n, i, j, k, sum, t, m, x, y, z, s, cas = 0;    scanf("%d", &t);    while (t--)    {        memset(flag, 0, sizeof(flag));        scanf("%d%d", &n, &m);        ans = 1e15;        for (i = 1;i <= n;i++) p[i].clear();        for (i = 1;i <= m;i++)        {            scanf("%d%d%d", &x, &y, &z);            if (x == y) continue;            node temp;            temp.v = y, temp.w = z;            p[x].push_back(temp);        }        scanf("%d", &k);        memset(dis, 1, sizeof(dis));        for (i = 1;i <= k;i++)        {            scanf("%d", &a[i]);            flag[a[i]] = 1;        }        for (i = 1;i <= k;i++)        {            s = a[i];            bfs(s);        }        memset(dis, 1, sizeof(dis));        for (i = k;i >= 1;i--)        {            s = a[i];            bfs(s);        }        printf("Case #%d: %d\n",++cas, ans);    }    return 0;}void bfs(int s){    queue<int>q;    dis[s] = 0;    q.push(s);    while (!q.empty())    {        int u = q.front();q.pop();        if (flag[u] && u != s) ans = min(ans, dis[u]);        for (int i = 0;i < p[u].size();i++)        {            int v = p[u][i].v;            if (dis[v] > dis[u] + p[u][i].w)            {                dis[v] = dis[u] + p[u][i].w;                q.push(v);            }        }    }}


原创粉丝点击