LightOJ 1019 Brush (V) 最短路裸题

题目:http://www.lightoj.com/volume_showproblem.php?problem=1019

题意:求从1到n的最短路。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

const int N = 110, INF = 0x3f3f3f3f;
struct edge
{
    int to, cost, next;
}g[N*N*2];
int cnt, head[N], dis[N];
bool vis[N];
int n, m, cas;
void add_edge(int v, int u, int cost)
{
    g[cnt].to = u, g[cnt].cost = cost, g[cnt].next = head[v], head[v] = cnt++;
    g[cnt].to = v, g[cnt].cost = cost, g[cnt].next = head[u], head[u] = cnt++;
}
int spfa(int s, int t)
{
    memset(dis, 0x3f, sizeof dis);
    memset(vis, 0, sizeof vis);
    queue<int> que;
    que.push(s), dis[s] = 0, vis[s] = true;
    while(! que.empty())
    {
        int v = que.front(); que.pop();
        vis[v] = false;
        for(int i = head[v]; i != -1; i = g[i].next)
        {
            int u = g[i].to;
            if(dis[u] > dis[v] + g[i].cost)
            {
                dis[u] = dis[v] + g[i].cost;
                if(! vis[u]) que.push(u), vis[u] = true;
            }
        }
    }
    return dis[t];
}
int main()
{
    int t, a, b, c;
    scanf("%d", &t);
    while(t--)
    {
        cnt = 0;
        memset(head, -1, sizeof head);
        scanf("%d%d", &n, &m);
        for(int i = 0; i < m; i++) scanf("%d%d%d", &a, &b, &c), add_edge(a, b, c);
        int res = spfa(1, n);
        if(res == INF) printf("Case %d: Impossible\n", ++cas);
        else printf("Case %d: %d\n", ++cas, res);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值