poj3255次短距离 dijkstra

虽然是水题, 但是一次照样写不对。

虽然是次短距离,但是是比最短距离短的距离,而不是小于等于最短距离的距离。


为什么这个dijkstra没用used标记,因为题目中说了,所有路可以走无限次


具体还是看代码吧


#include <cstdio>
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

//结构体
typedef struct edge
{
    int to, cost;
    edge(int to1, int cost1){to = to1; cost = cost1;}
}edge;

typedef pair<int, int> P;

//常量
const int N_MAX = 5005;
const int R_MAX = 100005;
const int INF = 1<<31-1;

//变量
int dist[N_MAX];
int dist2[N_MAX];
int N,R;  //N个点,R个边
vector<edge> V[N_MAX];

//函数

int solve()
{

    priority_queue<P, vector<P>, greater<P> > pque;
    pque.push(P(1,0));
    while(!pque.empty())
    {
        P temp = pque.top();
        pque.pop();

        int v = temp.first;
        int d = temp.second;

        if(dist2[v] < d)continue;

        for(int i=0; i<V[v].size(); i++)
        {
            int d2 = d + V[v][i].cost;
            int to = V[v][i].to;

            if(dist[to]>d2)
            {
                swap(dist[to],d2);
                pque.push(P(to,dist[to]));
            }
            if(dist2[to] > d2)
            {
                dist2[to] = d2;
                pque.push(P(to,d2));
            }
        }
    }
}

int main()
{
    while(scanf("%d%d",&N, &R)!=EOF)
    {
        fill(dist,dist+N+1,INF);
        fill(dist2,dist2+N+1,INF);
        for(int i=1;i<=N; i++)V[i].clear();
        int x, y, c;
        for(int i=0; i<R; i++)
        {
            scanf("%d%d%d", &x, &y, &c);
            V[x].push_back(edge(y,c));
            V[y].push_back(edge(x,c));
        }
        solve();
        cout << dist2[N];
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值