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

来源:互联网 发布:网卡mac地址怎么恢复 编辑:程序博客网 时间:2024/06/05 10:06

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

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

Example Output

3 40

#include <iostream>#include <cstdio>#include <cstring>using namespace std;int e[1000][1000],m[1000][1000];int book[1000],dis[1000],cost[1000];int N,M,s,d;int inf = 99999999;void Dijkstra(){    int i,j,k;    for(i=0;i<N;i++)    {        dis[i] = e[s][i];        cost[i] = m[s][i];    }    book[s] = 1;    for(i=1;i<=N-1;i++)    {        int minx1 = inf;        int minx2 = inf;        for(j=0;j<N;j++)        {            if((book[j] == 0 && dis[j] < minx1 )|| (book[j] == 0 && dis[j] == minx1 && cost[j]<minx2))            {                minx1 = dis[j];                minx2 = cost[j];                k = j;            }        }        book[k] = 1;        for(j = 0;j<N;j++)        {            if(e[k][j] < inf)            {                if((book[j] == 0 && dis[j] > dis[k] + e[k][j])||(book[j] == 0&&dis[j] == dis[k] + e[k][j]&&cost[j] > cost[k] + m[k][j] ))                {                    dis[j] = dis[k] + e[k][j];                    cost[j] = cost[k] + m[k][j];                }            }        }    }    printf("%d %d\n",dis[d],cost[d]);}int main(){    int T;    int a,b,l,p;    scanf("%d",&T);    while(T--)    {        memset(book,0,sizeof(book));        scanf("%d%d%d%d",&N,&M,&s,&d);        for(int i=0;i<N;i++)        {            for(int j=0;j<N;j++)            {                if(i == j)                {                    e[i][j] = 0;                    m[i][j] = 0;                }                else                {                    e[i][j] = inf;                    m[i][j] = inf;                }            }        }        for(int i=1;i<=M;i++)        {            scanf("%d%d%d%d",&a,&b,&l,&p);            e[a][b] = e[b][a] = l;            m[a][b] = m[b][a] = p;        }        Dijkstra();    }    return 0;}
阅读全文
0 0
原创粉丝点击