数据结构实验之图论七:驴友计划

Time Limit: 1000 ms           Memory Limit: 65536 KiB

Submit Statistic Discuss

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 算法 )的更多相关文章

  1. SDUT 3363 数据结构实验之图论七:驴友计划

    数据结构实验之图论七:驴友计划 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 做为一个资深 ...

  2. 数据结构实验之图论七:驴友计划【迪杰斯特拉算法】(SDUT 3363)

    分析:可以求简单的任意两点间最短距离的稍微变形,一个板子题.  #include <iostream> #include <bits/stdc++.h> using names ...

  3. SDUT OJ 数据结构实验之图论八:欧拉回路

    数据结构实验之图论八:欧拉回路 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...

  4. SDUT 3364 数据结构实验之图论八:欧拉回路

    数据结构实验之图论八:欧拉回路 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 在哥尼斯堡的 ...

  5. SDUT 3346 数据结构实验之二叉树七:叶子问题

    数据结构实验之二叉树七:叶子问题 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 已知一个按 ...

  6. SDUT OJ 数据结构实验之图论十:判断给定图是否存在合法拓扑序列

    数据结构实验之图论十:判断给定图是否存在合法拓扑序列 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Prob ...

  7. SDUT OJ 数据结构实验之图论六:村村通公路(最小生成树)

    数据结构实验之图论六:村村通公路 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...

  8. SDUT OJ 数据结构实验之图论五:从起始点到目标点的最短步数(BFS)

    数据结构实验之图论五:从起始点到目标点的最短步数(BFS) Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss P ...

  9. SDUT OJ 数据结构实验之图论四:迷宫探索

    数据结构实验之图论四:迷宫探索 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...

随机推荐

  1. JBPM具体应用之decision节点的使用

    JBPM工作流引擎为我们提供了许多的节点应用,每一个节点都有其不同的作用,其中有四个比较常用的节点,他们分别decision,fork,state和task.在本文中我们先介绍decision节点,余 ...

  2. 可变、不可变数据类型和hash

    一.可变和不可变数据类型 在python中,我们对数据类型除了分为数字类型.字符串类型.列表类型.元组类型.字典类型和集合类型外, 还有另外一种分类方式,我们给数据类型分为可变数据类型和不可变数据类型 ...

  3. 解决mybatis中转义字符的问题

    xml格式中不允许出现类似“>”这样的字符,有如下两种解决方法 方法一:使用转义字符 SELECT * FROM test WHERE 1 = 1 AND start_date <= CU ...

  4. Dom对象与jQuery对象的转换

  5. Oracle 环境下 GoldenGate 集成抽取(Integrated Capture)模式与传统抽取模式(Classic Capture)间的切换

    检查抽取进程模式 在 GGSCI 环境下,执行类似如下语句查看特定进程的状态. GGSCI> info <Group_Name> 其中,<Group_Name> 为进程名 ...

  6. Tarjan的强连通分量算法

    Tarjan算法用于寻找图G(V,E)中的所有强连通分量,其时间复杂度为O(|V|+|E|). 所谓强连通分量就是V的某个极大子集,其中任意两个结点u,v在图中都存在一条从u到v的路径. Tarjan ...

  7. laravel中的文件上传到本地+七牛云上传

    首先在filesystems.php 配置好上传的文件的目录起名为upload 在Storage/目录下面 目录下面的app/upload 如果没有这个文件会自动创建 这里的名字upload名字是跟控 ...

  8. 关于getchar的一些思考

    这个问题是有一段代码引起的: 代码1: #include<iostream> using namespace std; int main() { char t; t=getchar(); ...

  9. selenium使用ChromeDriver

    什么是ChromeDriver? ChromeDriver是Chromium team开发维护的,它是实现WebDriver有线协议的一个单独的服务.ChromeDriver通过chrome的自动代理 ...

  10. c++虚析构函数的使用及其注意点

    // ConsoleApplication33.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream& ...