hdu 6166 Senior Pan(最短路)

Senior Pan

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


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



解:把连接所给集合中的点的边删掉并记录最小值,因为最后的答案中一定不会通过这条边,如果通过这条边那么就不是最优的;

因为不知道起点所以就枚举起点,把所给序列正反各求一遍最短路,因为起点和终点 在序列中的先后顺序不知道



#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<map>
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
typedef long long LL;
const LL mod = 1e9+7;
const int inf = 0x3f3f3f3f;
struct node
{
    int to;
    LL w;
};
vector<node>p[N];
int a[N], in[N], vis[N];
LL dist[N];
queue<int>q;

int main()
{
    int t, ncase=1;
    scanf("%d", &t);
    while(t--)
    {
        int n, m, k;
        scanf("%d %d", &n, &m);
        node tmp;
        for(int i=1;i<=n;i++) p[i].clear();
        for(int i=0;i<m;i++)
        {
            int x, y;
            LL z;
            scanf("%d %d %lld", &x, &y, &z);
            tmp.to=y,tmp.w=z;
            p[x].push_back(tmp);
        }
        scanf("%d", &k);
        memset(in,0,sizeof(in));
        for(int i=0;i<k;i++)
            scanf("%d", &a[i]), in[a[i]]++;
        LL ans=10000000000+5LL;
        for(int i=0;i<k;i++)
        {
            for(int j=0;j<p[a[i]].size();j++)
            {
                tmp=p[a[i]][j];
                if(in[tmp.to])
                {
                    ans=min(ans,tmp.w);
                    p[a[i]].erase(p[a[i]].begin()+j);
                    j--;
                }
            }
        }
        memset(dist,-1,sizeof(dist));
        memset(vis,0,sizeof(vis));
        while(!q.empty()) q.pop();
        for(int i=0;i<k;i++)
        {
            if(dist[a[i]]!=-1) ans=min(ans,dist[a[i]]);
            dist[a[i]]=0;
            q.push(a[i]);
            while(!q.empty())
            {
                int u=q.front();q.pop();
                vis[u]=0;
                for(int j=0;j<p[u].size();j++)
                {
                    int v=p[u][j].to;
                    if(dist[v]==-1||dist[v]>dist[u]+p[u][j].w)
                    {
                        dist[v]=dist[u]+p[u][j].w;
                        if(!vis[v]) vis[v]=1,q.push(v);
                    }
                }
            }
        }
        memset(dist,-1,sizeof(dist));
        memset(vis,0,sizeof(vis));
        for(int i=k-1;i>=0;i--)
        {
            if(dist[a[i]]!=-1) ans=min(ans,dist[a[i]]);
            dist[a[i]]=0;
            q.push(a[i]);
            while(!q.empty())
            {
                int u=q.front();q.pop();
                vis[u]=0;
                for(int j=0;j<p[u].size();j++)
                {
                    int v=p[u][j].to;
                    if(dist[v]==-1||dist[v]>dist[u]+p[u][j].w)
                    {
                        dist[v]=dist[u]+p[u][j].w;
                        if(!vis[v]) vis[v]=1,q.push(v);
                    }
                }
            }
        }
        printf("Case #%d: %lld\n",ncase++,ans);
    }
    return 0;
}







评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值