spfa hdu 2066 一个人的旅行

/*c++ 62ms
 *g++ 78ms
 *spfa+暴力
 */
#include <bits/stdc++.h>
#define MEM(a,x) memset(a , x , sizeof(a))
using namespace std;

typedef int weight_t;
const int SIZE_E = 1000000;
const int SIZE_V = 1050;
struct edge_t{
    int v;
    weight_t w;
    edge_t *next;
}Edge[SIZE_E];

edge_t *V[SIZE_V];
int n;
int ECnt = 0;

void mkEdge(int const &a,int const &b,weight_t const &w){
    Edge[ECnt].v = b;
    Edge[ECnt].w = w;
    Edge[ECnt].next = V[a];
    V[a] = Edge+ECnt++;

    Edge[ECnt].v = a;
    Edge[ECnt].w = w;
    Edge[ECnt].next = V[b];
    V[b] = Edge+ECnt++;
}

weight_t D[SIZE_V];
bool flag[SIZE_V];
void spfa(int s){

    MEM(flag,0);
    fill(D,D+SIZE_V,INT_MAX);

    D[s] = 0;
    flag[s] = true;

    queue<int>q;
    q.push(s);

    while(!q.empty()){
        int u = q.front();
        q.pop();
        flag[u] = false;

        for(edge_t *p = V[u];p;p = p->next){

            int v = p->v;
            weight_t temp = D[u] + p->w;

            if (temp < D[v]){
                D[v] = temp;
                if (!flag[v]){
                    flag[v] = true;
                    q.push(v);
                }
            }
        }
    }
}
void init(){
    ECnt = 0;
    memset(V,0,sizeof(V));
    MEM(Edge,0);
}

int liveWith[SIZE_V];
int WantTo[SIZE_V];
int main(){
    int T,S,d;
    while(scanf("%d%d%d",&T,&S,&d)!=EOF){
        init();
        int a,b;
        weight_t time;
        for (int i = 0;i < T;++i){
            scanf("%d%d%d",&a,&b,&time);
            mkEdge(a,b,time);
        }
        for (int i = 0;i < S;++i)
            scanf("%d",liveWith+i);
        for (int i = 0;i < d;++i)
            scanf("%d",WantTo+i);
        int ans = INT_MAX;
        //直接爆搜所有情况
        for (int i = 0;i < S;++i){
            for (int j = 0;j < d;++j){
                spfa(liveWith[i]);
                ans = min(ans,D[WantTo[j]]);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值