HDU 3790 最短路径问题 (SPFA)
转载请注明出处:http://blog.csdn.net/a1dark
分析:比一般最短路多了一个花费、多加一个判断即可、用的SPFA、这道题让我搞清楚了以前定义INF为啥爆的问题、受益颇多、
#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
#define INF 0x7fffffff
#define N 1005
struct node{
int len,cost;
}map[N][N];
node dist[N];
int vis[N];
int m,n;
void spfa(int x){
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++){
dist[i].len=INF;
dist[i].cost=INF;
}
dist[x].len=0;
dist[x].cost=0;
queue<int >q;
q.push(x);
vis[x]=1; while(!q.empty()){
int now=q.front();
q.pop();
vis[now]=0;
for(int i=1;i<=n;i++){
if(map[now][i].len+dist[now].len<dist[i].len&&map[now][i].len!=INF){
dist[i].len=map[now][i].len+dist[now].len;
printf("%d\n",map[now][i].len);
dist[i].cost=map[now][i].cost+dist[now].cost;
if(!vis[i]){
q.push(i);
vis[i]=1;
}
}
if(map[now][i].len+dist[now].len==dist[i].len&&map[now][i].cost+dist[now].cost<dist[i].cost){
map[now][i].cost+dist[now].cost==dist[i].cost;
}
}
}
}
void init(){
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
if(i==j){
map[i][j].len=0;
map[i][j].cost=0;
}
else{
map[i][j].len=INF;
map[i][j].cost=INF;
}
}
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
if(n==0&&m==0)break;
init();
int s,e,d,p;
for(int i=1;i<=m;i++){
scanf("%d%d%d%d",&s,&e,&d,&p);
if(d<map[s][e].len){
map[s][e].len=d;
map[e][s].len=d;
map[s][e].cost=p;
map[e][s].cost=p;
}
else if(d==map[s][e].len&&p<map[s][e].cost){
map[s][e].cost=p;
map[e][s].cost=p;
}
}
int st,ed;
scanf("%d%d",&st,&ed);
spfa(st);
printf("%d %d\n",dist[ed].len,dist[ed].cost);
}
return 0;
}
HDU 3790 最短路径问题 (SPFA)的更多相关文章
- ACM: HDU 3790 最短路径问题-Dijkstra算法
HDU 3790 最短路径问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Des ...
- HDU - 3790 最短路径问题 (dijkstra算法)
HDU - 3790 最短路径问题 Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费 ...
- HDU 3790 最短路径问题(SPFA || Dijkstra )
题目链接 题意 : 中文题不详述. 思路 :无论是SPFA还是Dijkstra都在更新最短路的那个地方直接将花费更新了就行,还有别忘了判重边,话说因为忘了判重边WA了一次. #include < ...
- HDU 3790 最短路径问题 (最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3790 简单的最短路问题,这题听说有重边.我用spfa和dijkstra写了一遍,没判重边,速度都差不多 ...
- HDU 3790最短路径问题 [最短路最小花费]
题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=3790] 最短路径问题 Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 3790 最短路径问题(双重权值,dijkstra算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3790 题目大意:题意明了,输出最短路径及其花费. 需要注意的几点:(1)当最短路径相同时,输出最小花费 ...
- hdu 3790 最短路径问题(两个限制条件的最短路)
http://acm.hdu.edu.cn/showproblem.php?pid=3790 有两个条件:距离和花费.首先要求距离最短,距离相等的条件下花费最小. dijkstra,仅仅是在推断条件时 ...
- hdu 3790 最短路径问题(迪杰斯特拉)
最短路径问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- #HDU 3790 最短路径问题 【Dijkstra入门题】
题目: 最短路径问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
随机推荐
- python中的迭代
#迭代Python的for循环不仅可以用在list或tuple上,还可以作用在其他可迭代对象上. #list这种数据类型虽然有下标,但很多其他数据类型是没有下标的,但是,只要是可迭代对象,无论有无下标 ...
- jQuery+Ajax+Jsp做二级级联
终于弄懂了这个级联,我去!必须得在博客记下来. 1, JS代码: $(document).ready(function(){ $("#select1").change(functi ...
- OCP-1Z0-053-V13.02-712新题
Why does the number of blocks for the table remain the sale after the shrink operation? A.Because ...
- Identity-修改Error错误提示为中文
第一步:重写IdentityErrorDescriber public class CustomIdentityErrorDescriber : IdentityErrorDescriber ...
- 工商管理硕士(MBA)-北大国际MBA
工商管理硕士(MBA)-北大国际MBA [EMBA校友跨届晚会]不管风雨彩虹 我们永远在一起
- C++ Primer 学习笔记_87_用于大型程序的工具 --异常处理
用于大型程序的工具 --异常处理 引言: C++语言包括的一些特征在问题比較复杂,非个人所能管理时最为实用.如:异常处理.命名空间和多重继承. 相对于小的程序猿团队所能开发的系统需求而言,大规模编程[ ...
- postgres-xc手册生成方法
步骤 检测编译环境 安装编译工具 编译 以上只在linux环境当中进行,本人所用系统ubuntu15.04 检测编译环境 在posgtgresql目录下运行./configure,并安装需要安 ...
- Intent过滤,intent-filter
Intent过滤 编写:kesenhoo - 原文:http://developer.android.com/training/basics/intents/filters.html 前两节课主要讲了 ...
- linux(Centos 6.3)学习笔记
一.系统分区 1,磁盘分区 使用分区编辑器(partition editor)在磁盘上划分几个逻辑部分.碟片一旦划分成 数个分区,不同类的目录与文件可以存储进不同 ...
- activebar的用法
效果图: 网站页面上弹出消息提示狂,用来提示重大事件. <script src="http://www.ijquery.cn/js/jquery-1.7.2.min.js"& ...