ACM题目————最短路径问题
Description
Input
(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题目————最短路径问题的更多相关文章
- HDU ACM 3790 最短路径问题
最短路径问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- ACM题目————中缀表达式转后缀
题目描述 我们熟悉的表达式如a+b.a+b*(c+d)等都属于中缀表达式.中缀表达式就是(对于双目运算符来说)操作符在两个操作数中间:num1 operand num2.同理,后缀表达式就是操作符在两 ...
- ACM之最短路径做题笔记与记录
在这里纪念一下从4月开始一直因为事情而荒废了的最短路,多亏了jbb的帮助,我才完成了FZU热身赛一题简单的一个用模拟链表存边以及最短路径的学习,目前(6.5)已经学会使用了最简单的djstral与sp ...
- 超强的ACM题目类型总结
转:初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. ...
- HDU ACM 题目分类
模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 104 ...
- ACM题目推荐(刘汝佳书上出现的一些题目)[非原创]
原地址:http://blog.csdn.net/hncqp/article/details/1758337 推荐一些题目,希望对参与ICPC竞赛的同学有所帮助. POJ上一些题目在http://16 ...
- 有一种acm题目叫做,奇葩!
本文全然没有技术含量,纯粹是娱乐. 我事实上想写点东西.可是近期好像做计算几何做得太多了,一种想说说不出东西的感觉,唯有写一下一些奇葩的题目了. HDU3337:Guess the number pi ...
- ACM题目————STL练习之求次数
题目地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=1112 描述 题意很简单,给一个数n 以及一个字符串str,区间[i,i+n-1] 为一个 ...
- ACM题目————zoj问题
题目1006:ZOJ问题 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:20322 解决:3560 题目描述: 对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC. ...
随机推荐
- 分布式环境下限流方案的实现redis RateLimiter Guava,Token Bucket, Leaky Bucket
业务背景介绍 对于web应用的限流,光看标题,似乎过于抽象,难以理解,那我们还是以具体的某一个应用场景来引入这个话题吧. 在日常生活中,我们肯定收到过不少不少这样的短信,“双11约吗?,千款….”,“ ...
- 浅谈C# 匿名变量
每次写博客,第一句话都是这样的:程序员很苦逼,除了会写程序,还得会写博客!当然,希望将来的一天,某位老板看到此博客,给你的程序员职工加点薪资吧!因为程序员的世界除了苦逼就是沉默.我眼中的程序员大多都不 ...
- tcpdump note
from http://www.cnblogs.com/ggjucheng/archive/2012/01/14/2322659.html 用简单的话来定义tcpdump,就是:dump the tr ...
- PostgreSQL表空间
postgres=# \h create tablespace Command: CREATE TABLESPACEDescription: define a new tablespaceSyntax ...
- Oracle-数据库
Oracle 1.特点 关系型数据库 采用二维表的行使管理数据库 具有行和列 表间存在关联关系 2.安装 数据库(11g) 版本类型 32位 64位 安装类型 桌面类 本机开发 服务器类 生 ...
- 【你吐吧c#每日学习】10.29 C#字符串类型&Common operators
backslash \反斜杠 escape sequence 转义字符 double quote 双引号 new line 新行字符 Bell アラート Console.WriteLine(" ...
- C#: 数据绑定
数据绑定是分离UI和后端主逻辑程序的一种好的办法.这里总结下TextBox, Label, ComboBox, ListBox, DataGridView的数据绑定 数据绑定都是通过DB来和UI控件的 ...
- JAVA通过poi对Excel数据在(jsp+ssh)环境下导入导出
POI的下载与安装 请到网站http://www.apache.org/dyn/closer.cgi/poi/右击超链接2.5.1.zip下载压缩包poi-bin-2.5.1-final-20040 ...
- sql except 用法,找两个表中非共同拥有的
;with tt as (select a.id as id from [dbo].[1234] a where a.id not in (select a.ProtocolID from Proto ...
- 转:Order&Shipping Transactions Status Summary
详细内容: http://blog.csdn.net/pan_tian/article/details/7696528 WSH_DELIVERY_DETAILS.Release_Status can ...