转载请注明出处: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)的更多相关文章

  1. ACM: HDU 3790 最短路径问题-Dijkstra算法

    HDU 3790 最短路径问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Des ...

  2. HDU - 3790 最短路径问题 (dijkstra算法)

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

  3. HDU 3790 最短路径问题(SPFA || Dijkstra )

    题目链接 题意 : 中文题不详述. 思路 :无论是SPFA还是Dijkstra都在更新最短路的那个地方直接将花费更新了就行,还有别忘了判重边,话说因为忘了判重边WA了一次. #include < ...

  4. HDU 3790 最短路径问题 (最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3790 简单的最短路问题,这题听说有重边.我用spfa和dijkstra写了一遍,没判重边,速度都差不多 ...

  5. HDU 3790最短路径问题 [最短路最小花费]

    题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=3790] 最短路径问题 Time Limit: 2000/1000 MS (Java/Others)  ...

  6. hdu 3790 最短路径问题(双重权值,dijkstra算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3790 题目大意:题意明了,输出最短路径及其花费. 需要注意的几点:(1)当最短路径相同时,输出最小花费 ...

  7. hdu 3790 最短路径问题(两个限制条件的最短路)

    http://acm.hdu.edu.cn/showproblem.php?pid=3790 有两个条件:距离和花费.首先要求距离最短,距离相等的条件下花费最小. dijkstra,仅仅是在推断条件时 ...

  8. hdu 3790 最短路径问题(迪杰斯特拉)

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

  9. #HDU 3790 最短路径问题 【Dijkstra入门题】

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

随机推荐

  1. 五毛的cocos2d-x学习笔记05-场景与场景动画,动作

    场景切换函数: Director->getInstance()->replaceScene(Scene*); Director->getInstance()->runWithS ...

  2. Oracle Hint用法总结

    1. /*+ALL_ROWS*/ 表明对语句块选择基于开销的优化方法,并获得最佳吞吐量,使资源消耗最小化. 例如: SELECT /*+ALL+_ROWS*/ EMP_NO,EMP_NAM,DAT_I ...

  3. poj2774之最长公共子串

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 18794   Accepted: 77 ...

  4. Laravel 5.1 ACL权限控制 一

    请自行添加命名空间,代码下载地址 https://github.com/caoxt/learngit 1.所需要用到的数据表 users(用户表).roles(角色表).role_user(用户角色对 ...

  5. drawInRect:withAttributes:

    - (void)drawRect:(CGRect)frame { NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle defa ...

  6. 在Visual Studio 2010中安装iGraph

    本安装步骤参考了:http://www.16kan.com/question/detail/182225.html Igraph actually does work with Visual C++ ...

  7. vim Ctags 和taglist安装和使用

    Ctags是一个用于从程序源代码树产生索引文件(或tag文件),从而便于文本编辑器来实现快速定位的实用工具.在产生的tag文件中,每一个tag的入口指向了一个编程语言的对象.这个对象可以是变量定义.函 ...

  8. (3)选择元素——(4)css选择器(CSS selectors)

    The jQuery library supports nearly all of the selectors included in CSS specifications 1 through 3, ...

  9. 周根项《一分钟速算》全集播放&amp;下载地址

    点击图片就可以观看 ↓↓↓↓↓↓↓↓ 第1章:指算法 周根项<一分钟速算>第1章:指算法 第一节 对手的认识 周根项<一分钟速算>第1章:指算法 第二节 个位数比十位数大1乘以 ...

  10. 怎样用Eclipse正确导入开源库AndroidStaggeredGrid

    今天带给大家的是怎样正确导入开源库AndroidStaggeredGrid的代码,大家在使用github上的开源控件时,多多少少都遇到过像AndroidStaggeredGrid这样的project结 ...