hdoj3790_最短路径问题(最短路径)

#include<cstdio>
#include<iostream>
#include<vector>
#include<string.h>
using namespace std;

struct edge{
    int npos, dis, cost;
};

vector<edge> edges[2010];
pair<int, int> res[2010];
bool used[2010];

void kruskal(int s, int t,int n)
{
    memset(used, false, sizeof(used));
    used[s] = true;
    fill(res, res + 2010, make_pair(999999,999999));
    for (int i = 0; i < edges[s].size(); i++)
        if( (res[edges[s][i].npos].first > edges[s][i].dis) || 
            (res[edges[s][i].npos].first == edges[s][i].dis)&&(res[edges[s][i].npos].second>edges[s][i].cost))
        {
            res[edges[s][i].npos].first = edges[s][i].dis;
            res[edges[s][i].npos].second = edges[s][i].cost;
        }
    while (1)
    {
        int npos, tdis= 999999, tcost = 999999;
        for (int i = 0; i < n;i++)
            if (!used[i])
                if ((res[i].first < tdis) || (res[i].first == tdis) && (res[i].second < tcost))
                {
                    npos = i;
                    tdis = res[i].first;
                    tcost = res[i].second;
                }
        if (npos == t)
        {
            printf("%d %d\n", tdis, tcost);
            return;
        }
        used[npos] = true;
        for (int i = 0; i < edges[npos].size(); i++)
        {
            int tpos = edges[npos][i].npos;
            if (!used[tpos])
                if ((res[tpos].first > tdis + edges[npos][i].dis) || (res[tpos].first == tdis + edges[npos][i].dis) && (res[tpos].second > tcost + edges[npos][i].cost))
                {
                    res[tpos].first = tdis + edges[npos][i].dis;
                    res[tpos].second = tcost + edges[npos][i].cost;
                }
        }
    }
}

int main()
{
    int n, m;
    while (scanf("%d %d",&n,&m)&&n)
    {
        for (int i = 0; i < n; i++)
            edges[i].clear();
        for (int i = 0; i < m; i++)
        {
            int a, b, d, p;
            scanf("%d %d %d %d", &a, &b, &d, &p);
            edges[a - 1].push_back(edge{b-1,d,p});
            edges[b - 1].push_back(edge{a-1,d,p});
        }
        int s, t;
        scanf("%d %d", &s, &t);
        kruskal(s-1,t-1,n);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值