HDU 3790 最短路径问题(SPFA || Dijkstra )
题意 : 中文题不详述。
思路 :无论是SPFA还是Dijkstra都在更新最短路的那个地方直接将花费更新了就行,还有别忘了判重边,话说因为忘了判重边WA了一次。
//
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
const int INF = << ; using namespace std; int n, m,s,t;
int cost[][] ,mapp[][];
int dist[],cost1[] ;
int vis[] ; void spfa(int src)
{
queue<int> Q ;
memset(vis,,sizeof(vis)) ;
for(int i = ; i <= n ; i++)
{
dist[i] = INF ;
cost1[i] = INF ;
}
dist[src] = ;
vis[src] = ;
cost1[src] = ;
Q.push(src) ;
while(!Q.empty())
{
int u = Q.front() ;
Q.pop() ;
vis[u] = ;
for(int i = ; i <= n ; i++)
{ if(dist[i] > dist[u] + mapp[u][i])
{
dist[i] = dist[u] + mapp[u][i] ;
cost1[i] = cost1[u] + cost[u][i] ;
if(!vis[i])
{
vis[i] = ;
Q.push(i) ;
}
}
else if(dist[i] == dist[u]+mapp[u][i])
{
if(cost1[i] > cost1[u] + cost[u][i])
{
cost1[i]=cost1[u] + cost[u][i];
if(!vis[i])
{
vis[i] = ;
Q.push(i) ;
}
} }
}
}
}
void Init()
{
for(int i = ; i <= n ; i++)
for(int j = ; j <= n ; j++)
{
if(i == j) mapp[i][j] = cost[i][j] = ;
else mapp[i][j] = cost[i][j] = INF ;
}
}
int main()
{
//
while(cin >> n >> m)
{
if(n == && m == ) break ;
int a, b , d , p ;
Init() ;
for(int i = ; i < m ; i++)
{
cin >> a >> b >> d >> p ;
if(mapp[a][b] > d)
{
mapp[a][b] = mapp[b][a] = d ;
cost[a][b] = cost[b][a] = p ;
}
else if(mapp[a][b] == d&&cost[a][b] > p)
cost[a][b] = cost[b][a] = p ;
}
cin >> s >> t ;
spfa(s) ;
printf("%d %d\n",dist[t],cost1[t]) ;
}
return ;
}
会神的Dijkstra
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 1001
using namespace std;
const int inf=<<; int g[maxn][maxn];
int cost[maxn][maxn];
int n,m,a,b,d,p,s,e;
bool vis[maxn];
int dis[maxn],c[maxn]; void inti()
{
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
if(i==j)
{
g[i][j]=;
cost[i][j]=;
}
else
{
g[i][j]=inf;
cost[i][j]=inf;
}
}
}
} void dijkstra(int str)
{
memset(vis,false,sizeof(vis));
for(int i=; i<=n; i++)
{
dis[i]=g[str][i];
c[i]=cost[str][i];
}
dis[str]=;
c[str]=; vis[str]=true;
for(int i=; i<n; i++)
{
int m=inf,x;
for(int y=; y<=n; y++) if(!vis[y]&&dis[y]<m) m=dis[x=y];
vis[x]=true;
for(int y=; y<=n; y++)
{
if(!vis[y]&&g[x][y]!=inf)
{
if(dis[y]>dis[x]+g[x][y])
{
dis[y]=dis[x]+g[x][y];
c[y]=c[x]+cost[x][y];
}
else if(dis[y]==dis[x]+g[x][y])
{
if(c[y]>c[x]+cost[x][y])
c[y]=c[x]+cost[x][y];
}
}
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==&&m==) break;
inti();
for(int i=; i<m; i++)
{
scanf("%d%d%d%d",&a,&b,&d,&p);
if(d<g[a][b])
{
g[a][b]=g[b][a]=d;
cost[a][b]=cost[b][a]=p;
} }
scanf("%d%d",&s,&e);
dijkstra(s);
printf("%d %d\n",dis[e],c[e]);
}
return ;
}
HDU 3790 最短路径问题(SPFA || Dijkstra )的更多相关文章
- HDU - 3790 最短路径问题 (dijkstra算法)
HDU - 3790 最短路径问题 Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费 ...
- #HDU 3790 最短路径问题 【Dijkstra入门题】
题目: 最短路径问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU 3790 最短路径问题【Dijkstra】
题意:给出n个点,m条边,每条边的长度d和花费p,给出起点和终点的最短距离和花费,求最短距离,如果有多个最短距离,输出花费最少的 在用dijkstra求最短距离的时候,再用一个f[]数组保存下最少花费 ...
- HDU 3790 最短路径问题 (SPFA)
转载请注明出处:http://blog.csdn.net/a1dark 分析:比一般最短路多了一个花费.多加一个判断即可.用的SPFA.这道题让我搞清楚了以前定义INF为啥爆的问题.受益颇多. #in ...
- ACM: HDU 3790 最短路径问题-Dijkstra算法
HDU 3790 最短路径问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Des ...
- hdu 3790 最短路径问题(双重权值,dijkstra算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3790 题目大意:题意明了,输出最短路径及其花费. 需要注意的几点:(1)当最短路径相同时,输出最小花费 ...
- hdu 3790 最短路径dijkstra(多重权值)
最短路径问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- HDU 3790 最短路径问题 (最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3790 简单的最短路问题,这题听说有重边.我用spfa和dijkstra写了一遍,没判重边,速度都差不多 ...
- hdu 3790 最短路径问题(两个限制条件的最短路)
http://acm.hdu.edu.cn/showproblem.php?pid=3790 有两个条件:距离和花费.首先要求距离最短,距离相等的条件下花费最小. dijkstra,仅仅是在推断条件时 ...
随机推荐
- [C#] 记-TinyMapper使用
What is TinyMapper TinyMapper - a tiny and quick object mapper for .Net. The main advantage is perfo ...
- MySQL 设置允许远程登录
1.修改数据表 可能是你的帐号不允许从远程登陆,只能在localhost.这个时候只要在 localhost 的那台电脑,登入MySQL后,更改 "MySQL" 数据库里的 &qu ...
- 27.some company's Spi Flash chip replace altera epcsxxx
由于altera公司的epcsxxx芯片比较贵,所以一般用其它公司的spi flash芯片代替也可以.据AlteraFAE描述:“EPCS器件也是选用某家公司的SPIFlash,只是中间经过Alter ...
- 89C51单片机实现的流水灯
/*******************************************Copyright: 2014.02.09.version1.0File name: led.cDescrip ...
- 仿照CREATE_FUNC实现CCLayer中的返回CCScene* 的静态函数,宏包装成CREATE_SCENE(XXLayer)
#define CREATE_SCENE(__TYPE__)\ CCScene *scene()\ { CCScene *scene=CCScene::create();\ __TYPE__ *lay ...
- 模仿cocos2dx 风格用工厂方法,实现class A,不使用宏,
class A { static A *create(); bool init(); }; A* A::create() { A *pRet=new A; if(pRet && pRe ...
- Teamwork-Week3 职责划分及团队分数分配原则
本组人数:5. 一.人员职责划分 PM:1 柴泽华 PM的职责: 1)根据项目范围.质量.时间与成本的综合因素的考虑,进行项目的总体规划与阶段计划. 2)设置项目组中的各种角色, ...
- 为边框应用图片 border-image
为边框应用图片 border-image 顾名思义就是为边框应用背景图片,它和我们常用的background属性比较相似.例如: background:url(xx.jpg) 10px 20px no ...
- [转载]解决zabbix在configure时候遇到的问题(Ubuntu)
http://hi.baidu.com/ryq1xiuluo/item/6d37e658f1b90b13db16351d ./configure --enable-server --enable-ag ...
- 网站中的专题页或者tag聚合页的权重不错
一.据最近的一些观察,觉得网站中的专题页或者tag聚合页的权重不错,因此多给网站制作一些专题页面,不仅有利于聚合站内的文章,更是绝对的原创内容,应该会受到百度的青睐.简评:关于权重的讨论,这篇无疑是很 ...