2017 Multi-University Training Contest 9 1006 Senior Pan spfa

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
  
  
1 5 6 1 2 1 2 3 3 3 1 3 2 5 1 2 4 2 4 3 1 3 1 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);
            }
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值