数据结构实验之图论七:驴友计划 ( 最短路径 Dijkstra 算法 )
数据结构实验之图论七:驴友计划
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
做为一个资深驴友,小新有一张珍藏的自驾游线路图,图上详细的标注了全国各个城市之间的高速公路距离和公路收费情况,现在请你编写一个程序,找出一条出发地到目的地之间的最短路径,如果有多条路径最短,则输出过路费最少的一条路径。
Input
连续T组数据输入,每组输入数据的第一行给出四个正整数N,M,s,d,其中N(2 <= N <= 500)是城市数目,城市编号从0~N-1,M是城市间高速公路的条数,s是出发地的城市编号,d是目的地的城市编号;随后M行,每行给出一条高速公路的信息,表示城市1、城市2、高速公路长度、收费额,中间以空格间隔,数字均为整数且不超过500,输入数据均保证有解。
Output
在同一行中输出路径长度和收费总额,数据间用空格间隔。
Sample Input
1
4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20
Sample Output
3 40
Dijkstra算法:
#include <iostream>
#include <queue>
#define INF 999999
#define ERROR -1
using namespace std;
int n, m, s, d;
struct node
{
int length;
int cost;
} G[1001][1001];
int Min, i, j;
int V, W;
bool collected[1001];
int dist[1001], spend[1001];
int FindMinDist( )
{
Min = INF;
for(i=0; i<n; i++)
if( !collected[i] && dist[i] < Min )
{
Min = dist[i];
V = i;
}
if( Min == INF )
V = ERROR;
return V;
}
void Dijkstra( )
{
while(1)
{
V = FindMinDist( );
if( V == ERROR )
break;
collected[V] = true;
for( W=0; W<n; W++ )
if( collected[W] == false )
{
if( dist[V] + G[V][W].length < dist[W] )
{
dist[W] = dist[V] + G[V][W].length;
spend[W] = spend[V] + G[V][W].cost;
}
else if( dist[V] + G[V][W].length == dist[W] )
{
if( spend[V] + G[V][W].cost < spend[W] )
{
dist[W] = dist[V] + G[V][W].length;
spend[W] = spend[V] + G[V][W].cost;
}
}
}
}
}
int main()
{
int t;
cin >> t;
while( t-- )
{
cin >> n >> m >> s >> d;
for( i=0; i<n; i++ )
{
collected[i] = false;
for(j=0; j<n; j++ )
{
if( i == j )
{
G[i][j].length = 0;
G[i][j].cost = 0;
}
else
{
G[i][j].length = INF;
G[i][j].cost = INF;
}
}
dist[i] = INF;
spend[i] = INF;
}
dist[s] = 0;
spend[s] = 0;
while( m-- )
{
int a, b, le, mo;
cin >> a >> b >> le >> mo;
if(le < G[a][b].length)
{
G[a][b].length = le;
G[b][a].length = le;
G[a][b].cost = mo;
G[b][a].cost = mo;
}
}
Dijkstra( );
cout << dist[d] << " " << spend[d] << endl;
}
return 0;
}
Floyd算法:
#include <iostream>
#include <string.h>
#define INF 99999
#define ERROR -1
using namespace std;
int n, m, s, d;
int length[101][101];
int cost[101][101];
int a[101][101], b[101][101];
void Floyd ( )
{
int k, i, j;
for( k=0; k<n; k++ )
{
for( i=0; i<n; i++ )
{
for( j=0; j<n; j++)
{
if( i!=j )
{
if( length[i][j] > length[i][k] + length[k][j] )
{
length[i][j] = length[i][k] + length[k][j];
cost[i][j] = cost[i][k] + cost[k][j];
}
else if( length[i][j] == length[i][k] + length[k][j] )
{
if( cost[i][j] > cost[i][k] + cost[k][j] )
{
cost[i][j] = cost[i][k] + cost[k][j];
}
}
}
}
}
}
}
int main()
{
int i, j;
int t;
cin >> t;
while ( t-- )
{
cin >> n >> m >> s >> d;
for( i=0; i<n; i++ )
{
for( j=0; j<n; j++ )
{
if( i!=j )
{
length[i][j] = INF;
cost[i][j] = INF;
}
else
{
length[i][j] = 0;
cost[i][j] = 0;
}
}
}
while ( m-- )
{
int a, b, x, y;
cin >> a >> b >> x >> y;
if( x < length[a][b] )
{
length[a][b] = x;
length[b][a] = x;
cost[a][b] = y;
cost[b][a] = y;
}
}
Floyd ( );
cout << length[s][d] << " " << cost[s][d] << endl;
}
return 0;
}
有关这两种算法:最短路径 Dijkstra算法 AND Floyd算法
数据结构实验之图论七:驴友计划 ( 最短路径 Dijkstra 算法 )的更多相关文章
- SDUT 3363 数据结构实验之图论七:驴友计划
数据结构实验之图论七:驴友计划 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 做为一个资深 ...
- 数据结构实验之图论七:驴友计划【迪杰斯特拉算法】(SDUT 3363)
分析:可以求简单的任意两点间最短距离的稍微变形,一个板子题. #include <iostream> #include <bits/stdc++.h> using names ...
- SDUT OJ 数据结构实验之图论八:欧拉回路
数据结构实验之图论八:欧拉回路 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...
- SDUT 3364 数据结构实验之图论八:欧拉回路
数据结构实验之图论八:欧拉回路 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 在哥尼斯堡的 ...
- SDUT 3346 数据结构实验之二叉树七:叶子问题
数据结构实验之二叉树七:叶子问题 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 已知一个按 ...
- SDUT OJ 数据结构实验之图论十:判断给定图是否存在合法拓扑序列
数据结构实验之图论十:判断给定图是否存在合法拓扑序列 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Prob ...
- SDUT OJ 数据结构实验之图论六:村村通公路(最小生成树)
数据结构实验之图论六:村村通公路 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...
- SDUT OJ 数据结构实验之图论五:从起始点到目标点的最短步数(BFS)
数据结构实验之图论五:从起始点到目标点的最短步数(BFS) Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss P ...
- SDUT OJ 数据结构实验之图论四:迷宫探索
数据结构实验之图论四:迷宫探索 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...
随机推荐
- Java微信公众平台开发(三)--接收消息的分类及实体的创建
转自:http://www.cuiyongzhi.com/post/41.html 前面一篇有说道应用服务器和腾讯服务器是通过消息进行通讯的,并简单介绍了微信端post的消息类型,这里我们将建立消息实 ...
- Easyui Datagrid 如何实现后台交互显示用户数据列表
转自:https://blog.csdn.net/Tomsheng321/article/details/50722571?utm_source=blogxgwz9 新手初学的时候可能有个疑问:如何在 ...
- SQL提取数据库表名,字段名等信息
-------提取数据库所有表的表名.字段名 -------在SQLserver 2000中测试 --查询所有用户表所有字段的特征 SELECT D.Name as TableName, A.colo ...
- Java占位符替换工具类
import java.util.HashMap; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFact ...
- Docker镜像加速 | Docker 中国源 | 仓库
镜像加速 | Docker 中国https://www.docker-cn.com/registry-mirror
- 【codevs2822】爱在心中
题目描述 Description “每个人都拥有一个梦,即使彼此不相同,能够与你分享,无论失败成功都会感动.爱因为在心中,平凡而不平庸,世界就像迷宫,却又让我们此刻相逢Our Home.” 在爱的国度 ...
- laravel使用ORM操作数据库
laravel使用ORM操作数据库 public function mode(){ //查询所有 $isok=Student::get(); 新增. (1) $isok=Student::create ...
- Tensorflow学习(练习)—CPU训练模型
Mask R-CNN - Train on Shapes Dataset This notebook shows how to train Mask R-CNN on your own dataset ...
- Luogu 4438 [HNOI/AHOI2018]道路
$dp$. 这道题最关键的是这句话: 跳出思维局限大胆设状态,设$f_{x, i, j}$表示从$x$到根要经过$i$条公路,$j$条铁路的代价,那么对于一个叶子结点,有$f_{x, i, j} = ...
- python之连接oracle数据库
环境: windows,python2.7 1.下载cx_Oracle 在windows下不要使用easy_install或者pip,因为这样安装不会同步环境,并报错: distutils.error ...