hdu 2680 Choose the best route 最短路

把终点当起点,起点当终点,存边的时候反着存就可以了。

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2680

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const int MAXN  = 1010;
const int INF = 0x3f3f3f3f;

bool vis[MAXN];
int pre[MAXN];
int cost[MAXN][MAXN];
int lowcost[MAXN];

void Init(){
    memset(vis,0,sizeof(vis));
    memset(pre,-1,sizeof(pre));
    memset(cost,0x3f,sizeof(cost));
    memset(lowcost,0x3f,sizeof(lowcost));
}

void Dijkstra(int n,int beg){
    lowcost[beg] = 0;
    for(int i=0;i<n;i++){
        int k = -1;
        int Min = INF;
        for(int j=0;j<n;j++){
            if(!vis[j]&&lowcost[j]<Min){
                Min = lowcost[j];
                k = j;
            }
        }
        if(k==-1)break;
        vis[k] = true;
        for(int j=0;j<n;j++){
            if(!vis[j]&&lowcost[j]>lowcost[k]+cost[k][j]){
                lowcost[j] = lowcost[k] + cost[k][j];
                pre[j] = k;
            }
        }
    }
}

void Input(){
    int n,m,s;
    while(~scanf("%d %d %d",&n,&m,&s)){
        Init();
        int tempa,tempb,tempr;
        for(int i=0;i<m;i++){
            scanf("%d %d %d",&tempa,&tempb,&tempr);
            tempa --; tempb --;
            if(cost[tempb][tempa]>tempr){
                cost[tempb][tempa] = tempr;
            }
        }
        Dijkstra(n,s-1);
        int w;
        int ans = INF;
        scanf("%d",&w);
        for(int i=0;i<w;i++){
            scanf("%d",&tempa);
            tempa --;
            if(lowcost[tempa]<ans)
                ans = lowcost[tempa];
        }
        if(ans==INF)
            puts("-1");
        else 
            printf("%d\n",ans);
    }
}

void File(){
    freopen("a.in","r",stdin);
    freopen("a.out","w",stdout);
}

int main(void){
//    File();
    Input();
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值