数据结构实验之图论七:驴友计划 ( 最短路径 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 ...
随机推荐
- 【知识结构】最强Web认证知识体系
花了些时间总结了下Web认证,以及各种方式的利弊和使用,后续后继续更新.文章转载请注明出处:https://www.cnblogs.com/pengdai/p/9144843.html -----20 ...
- @Value关于static字段的注入
@Component public class BaseCode { //应用key public static String APP_KEY; //应用密钥 public static String ...
- spring mvc default-servlet mvc:resources mvc:default-servlet-handler区别
mvc:default-servlet-handler其实就是default-servlet 交由web容器自己处理 mvc:resources spring来处理 没有被映射的url web容器来处 ...
- xcode添加build phase
[xcode添加build phase] xcode版本:5.0.2,找了半天,终于找到add build phase的方法,如下图.
- ZOJ3953 Intervals
题意 有n个区间,要求删除一些区间使得不存在三个相交的区间.找出删除的最少区间. 分析 是个比较显然的贪心吧. 先按照区间的左起点进行排序,然后从左往右扫,当有三个区间相交的时候,删除那个右端点最远的 ...
- Python程序调试-TabError: inconsistent use of tabs and spaces in indentation
报错信息:TabError: inconsistent use of tabs and spaces in indentation 说明:代码缩进统一使用Tab键或空格键,不能混用. 解决办法: 1. ...
- loj2512 [BJOI2018]链上二次求和
传送门 分析 咕咕咕 代码 #include<iostream> #include<cstdio> #include<cstring> #include<st ...
- BZOJ3223 文艺平衡树(splay)
题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...
- C# static 字段初始值设定项无法引用非静态字段、方法或属性
问题:字段或属性的问题字段初始值设定项无法引用非静态字段.方法 下面代码出错的原因,在类中定义的字段为什么不能用? public string text = test(); //提示 字段或属性的问题 ...
- 21天学通C++学习笔记(四):数组和字符串
1. 数组 概念 是一组元素 这些元素是相同的数据类型 按顺序存储到内存中 目的是避免在业务需要时去重复声明很多同类型的变量 初始化 分别初始化:int i [5] = {1,2,3,4,5}; 全部 ...