Description

给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。

Input

输入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)

Output

输出 一行有两个数, 最短距离及其花费。

Sample Input

3 2
1 2 5 6
2 3 4 5
1 3
0 0

Sample Output

9 11

经典迪杰斯卡尔算法求最短路径,稍微加了一点小改动。适合像我这种入门的菜鸟!

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <stack>
#include <cmath>
#include <queue>
#define INF 0xffffff
using namespace std;
const int maxn = ;
int n, m, s, t, a, b, d, p;
int G[maxn][maxn];//距离
int pd[maxn][maxn];//金钱
int dis[maxn];//距离
int sp[maxn];//花费
bool vis[maxn];//记录是否走过 void Dij(){
dis[s] = ;
sp[s] = ; for(int k=; k<=n; k++){
int Min = INF ;
int ind = - ;
for(int i=; i<=n; i++){
if( !vis[i] && dis[i]<Min){
ind = i ;
Min = dis[i];
}
}
vis[ind] = true; for(int j=; j<=n; j++){
if( !vis[j] && dis[ind]+G[ind][j]<dis[j]){
dis[j] = dis[ind] + G[ind][j];
sp[j] = sp[ind] + pd[ind][j];
}
else if( !vis[j] && dis[j] == dis[ind]+G[ind][j] ){
sp[j] = min(sp[j],sp[ind]+pd[ind][j]);
}
}
}
} int main(){
while( scanf("%d %d",&n, &m) !=EOF ){
if( n == && m == ) break;
for(int i=; i<=n; i++){
vis[i] = false ;
sp[i] = INF ;
dis[i] = INF ;
for(int j=; j<=n; j++){
G[i][j] = INF ;
}
}
while( m -- ){
scanf("%d %d %d %d",&a, &b, &d, &p);
G[a][b] = G[b][a] = d ;
pd[a][b] = pd[b][a] = p ;
}
scanf("%d %d", &s, &t);
Dij();
cout << dis[t] << " " << sp[t] << endl ;
}
return ;
}

ACM题目————最短路径问题的更多相关文章

  1. HDU ACM 3790 最短路径问题

    最短路径问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  2. ACM题目————中缀表达式转后缀

    题目描述 我们熟悉的表达式如a+b.a+b*(c+d)等都属于中缀表达式.中缀表达式就是(对于双目运算符来说)操作符在两个操作数中间:num1 operand num2.同理,后缀表达式就是操作符在两 ...

  3. ACM之最短路径做题笔记与记录

    在这里纪念一下从4月开始一直因为事情而荒废了的最短路,多亏了jbb的帮助,我才完成了FZU热身赛一题简单的一个用模拟链表存边以及最短路径的学习,目前(6.5)已经学会使用了最简单的djstral与sp ...

  4. 超强的ACM题目类型总结

    转:初期: 一.基本算法:       (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.       ...

  5. HDU ACM 题目分类

    模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 104 ...

  6. ACM题目推荐(刘汝佳书上出现的一些题目)[非原创]

    原地址:http://blog.csdn.net/hncqp/article/details/1758337 推荐一些题目,希望对参与ICPC竞赛的同学有所帮助. POJ上一些题目在http://16 ...

  7. 有一种acm题目叫做,奇葩!

    本文全然没有技术含量,纯粹是娱乐. 我事实上想写点东西.可是近期好像做计算几何做得太多了,一种想说说不出东西的感觉,唯有写一下一些奇葩的题目了. HDU3337:Guess the number pi ...

  8. ACM题目————STL练习之求次数

    题目地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=1112 描述 题意很简单,给一个数n 以及一个字符串str,区间[i,i+n-1] 为一个 ...

  9. ACM题目————zoj问题

    题目1006:ZOJ问题 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:20322 解决:3560 题目描述: 对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC. ...

随机推荐

  1. isKindOfClass和isMemberOfClass

    https://github.com/ming1016/study/wiki/Objc-Runtime 先看看isKindOfClass和isMemberOfClass在Object.mm中的实现 - ...

  2. IIS URL Rewrite redirect from one Domain to another

    IIS URL Rewrite enables Web administrators to create powerful rules to implement URLs that are easie ...

  3. Plugin with id 'com.android.application' not found.

    构建报错: Error:(1, 0) Plugin with id 'com.android.application' not found. <a href="openFile&quo ...

  4. PPTP部署文档

    PPTP部署文档 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.       欢迎加入:高级运维工程师之路 598432640 前言:这款VPN部署起来特别简单,想对OPENVON配 ...

  5. 解决 linux [Fedora] 升级 导致VMware启动出现"before you can run vmware workstation, serveral modules must be complied and loaded into the runing kernel" 而无法卸载

    解决: 开机启动 进入 升级之前的内核系统 然后 执行卸载 VMware 命令 # vmware-uninstall You have gotten this message because you ...

  6. 记录把方法添加到 JavaScript 对象不明白的地方

    <!DOCTYPE html> <html> <body> <script> function person(firstname,lastname,ag ...

  7. fences(桌面整理软件)与eDiary3.3.3下载链接

    fences:  http://www.jb51.net/softs/309746.html http://jingyan.baidu.com/article/e8cdb32b6e958337042b ...

  8. javascript 内置对象

    什么是对象 javascript中的所有事物都是对象:字符串  数组  数值  函数... 每个对象都带有属性和方法 javascript允许自定义对象      自定义对象: 定义并创建对象实例 使 ...

  9. springday02-go4

    1.复制xml到container/annotation下2.新建Waiter类,构造函数,初始化以及销毁函数3.在Waiter方法体前面加上@Component4.xml中添加组件扫描代码5.tes ...

  10. JS获取项目根目录

    function getRootPath(){ //获取当前网址,如: http://localhost:8088/test/test.jsp var curPath=window.document. ...