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

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. CBV中的dispatch

    之前介绍了FBV和CBV ,下面我们看一下CBV中的dispatch dispatch函数在类View中定义,作用就是通过反射查找get或post函数,所以在执行get或post函数之前,dispat ...

  2. webRTC peerconnection_client demo创建VS工程

    编译了webRTC Windows源码之后,想使用编译出来的库写一个demo出来,但是又不知到怎么下手.就想通过源码中带的示例peerconnection_client和peerconnection_ ...

  3. Libevent使用例子,从简单到复杂

    转载请注明出处:http://blog.csdn.net/luotuo44/article/details/39670221 本文从简单到复杂,展示如何使用libevent.网上的许多例子都是只有服务 ...

  4. Animation Parameter

    [Animation Parameter] Animation Parameters are variables that are defined within the animation syste ...

  5. PHP屏蔽关键字实现方法

    方法一: 思路 用正则去匹配关键字,把关键字用别的字符替换: 1 2 3 $str = "/你大爷|你麻痹|什么玩意|SB|你他妈/";  // 关键字正则字符串  $string ...

  6. DataSet、DataTable转换List(泛型集合与DataSet互相转换 )

    using System.Data; using System.Reflection; using System.Collections; using System.Collections.Gener ...

  7. ajax load事件

    ajax.load  使用ajax 来改变div元素文本. load( url [, data][, callback] ) load()方法的传递方式根据参数data来自动指定.如果没有参数传递,则 ...

  8. ZIP压缩格式与RAR压缩格式

    早已习惯了安装系统之后必须安装winrar,压缩文件也已经习惯了rar格式,这种习惯的力量真的挺可怕的.在工作中你的同事可能没有安装winrar,或者他们不喜欢安装盗版软件,这时候你给他们发送过去的是 ...

  9. IntelliJ IDEA——利用maven插件构建web工程

  10. 单元测试NUnit 的文章

    请参考 https://www.cnblogs.com/ranh941/p/7629279.htmlhttps://blog.csdn.net/qincode/article/details/1831 ...