BFS,dijkstra算法

http://ac.jobdu.com/problem.php?pid=1008

题目描述:
给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。
输入:
输入n,m,点的编号是1~n,然后是m行,每行4个数 a,b,d,p,表示a和b之间有一条边,且其长度为d,花费为p。最后一行是两个数 s,t;起点s,终点t。n和m为0时输入结束。
(1<n<=1000, 0<m<100000, s != t)
输出:
输出 一行有两个数, 最短距离及其花费。
样例输入:
3 2
1 2 5 6
2 3 4 5
1 3
0 0
样例输出:

9 11


代码贡献者:http://my.csdn.net/cstur4

#include <iostream>
#include <string>
#include <algorithm>
#include <memory.h>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <vector>
using namespace std;
#define MAX 0Xfffffff
struct Node{
    int p, d;
    int th;
    bool operator>(const Node&node)const{
        if(d==node.d)
            return p>node.p;
        return d>node.d;
    }
};
bool visit[1001];
int mapd[1001][1001];
int mapp[1001][1001];
int dis[1001];
int ps[1001];
priority_queue<Node, vector<Node>, greater<Node>> q;  //应该使用小顶堆

int main(){

    //freopen("in.txt", "r", stdin);
    
    int n, m;
    int a, b, d, p;
    while(cin>>n>>m, n+m){
        for(int i=1;i<=n;++i){
            visit[i] = false;
            dis[i] = MAX;
            ps[i] = MAX;
            for(int j=1;j<=n;++j){
                if(i==j){
                    mapd[i][j] = mapp[i][j] = 0;
                    continue;
                }
                mapd[i][j] = MAX;
                mapp[i][j] = MAX;
            }
        }

        for(int i=0;i<m;++i){
            scanf("%d%d%d%d", &a, &b, &d, &p);
            if(d<mapd[a][b]){
                mapd[a][b] = mapd[b][a] = d;
                mapp[a][b] = mapp[b][a] = p;
            }
			if(d==mapd[a][b]&&p<mapp[a][b]){
				mapp[a][b] = mapp[b][a] = p;
			}
        }
        int s, t;
        cin>>s>>t;
        Node node;
        node.th = s;
        node.d = 0;
        node.p = 0;
        q.push(node);
        while(!q.empty()){               //BFS, kijstra算法求最小距离,其中有dp的思想
            Node node = q.top();
            q.pop();
            if(visit[node.th])
                continue;
            visit[node.th] = true;
            for(int i=1;i<=n;++i){
                if(i!=node.th && mapd[node.th][i]!=MAX && visit[i]==false){ 
                    if(node.d + mapd[node.th][i] < dis[i]){
                        dis[i] = node.d + mapd[node.th][i];
                        ps[i] = node.p + mapp[node.th][i];
                        Node tmp;
                        tmp.th = i;
                        tmp.d = dis[i];
                        tmp.p = ps[i];
                        q.push(tmp);
                    }else if(node.d + mapd[node.th][i] == dis[i] && node.p + mapp[node.th][i] < ps[i]){
                        ps[i] = node.p + mapp[node.th][i];
                        Node tmp;
                        tmp.d = dis[i];
                        tmp.p = ps[i];
                        tmp.th = i;
                        q.push(tmp);
                    }
                }
            }

        }
		if( s==t )
			printf("%d %d\n", 0, 0);            
		else
			printf("%d %d\n", dis[t], ps[t]);

    }
    
    //fclose(stdin);
}   


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值