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

来源:互联网 发布:网站采集软件 编辑:程序博客网 时间:2024/06/07 23:16

结合了一下大神的博客,但是我忘记了出处,sorry.

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

Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic

Problem Description

做为一个资深驴友,小新有一张珍藏的自驾游线路图,图上详细的标注了全国各个城市之间的高速公路距离和公路收费情况,现在请你编写一个程序,找出一条出发地到目的地之间的最短路径,如果有多条路径最短,则输出过路费最少的一条路径。

Input

连续T组数据输入,每组输入数据的第一行给出四个正整数N,M,s,d,其中N(2 <= N <= 500)是城市数目,城市编号从0~N-1,M是城市间高速公路的条数,s是出发地的城市编号,d是目的地的城市编号;随后M行,每行给出一条高速公路的信息,表示城市1、城市2、高速公路长度、收费额,中间以空格间隔,数字均为整数且不超过500,输入数据均保证有解。 

Output

在同一行中输出路径长度和收费总额,数据间用空格间隔。 

Example Input

14 5 0 30 1 1 201 3 2 300 3 4 100 2 2 202 3 1 20

Example Output

3 40

Hint

#include<bits/stdc++.h>using namespace std;#define inf 0x3f3f3f3fint n, m, s, d;int cost[505][505], hua[505][505];int Map[505][505], dis[505][505];void floyd(){    int i, j, k;    for(i = 0; i < n; i++)    {        for(j = 0; j < n; j++)        {            dis[i][j] = Map[i][j];            cost[i][j] = hua[i][j];        }    }    for(k = 0; k < n; k++)        for(i = 0; i < n; i++)        for(j = 0; j < n; j++)    {        if(dis[i][j]>dis[i][k]+dis[k][j])            {               dis[i][j] = dis[i][k]+dis[k][j];               cost[i][j] += cost[i][k]+cost[k][j];            }        else if(dis[i][j]==(dis[i][k]+dis[k][j]))        {            if(cost[i][j]>(cost[i][k]+cost[k][j]))                cost[i][j] = cost[i][k]+cost[k][j];        }    }}int main(){    int t, i, j, a, b, x, y;    scanf("%d", &t);    while(t--)    {        memset(Map, inf, sizeof(Map));        memset(hua, 0, sizeof(hua));         for(i = 0, j = 0; i<n; i++)        {            if(i==j)           Map[i][j] = 0;        }        scanf("%d%d%d%d", &n, &m, &s, &d);        for(i = 0; i < m; i++)        {            scanf("%d%d%d%d", &a, &b, &x, &y);            Map[a][b] = Map[b][a] = x;            hua[a][b] = hua[b][a] = y;        }        floyd();        cout<<dis[s][n-1]<<" "<<cost[s][n-1]<<endl;    }}


原创粉丝点击