PAT 甲级真题 1003 Emergency (25分) c语言实现

6 篇文章 0 订阅

1003 Emergency (25分)

As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

Input Specification:
Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (≤500) - the number of cities (and the cities are numbered from 0 to N−1), M - the number of roads, C1 and C2 - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c​1, c​2 and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C​1 to C​2
​​ .

Output Specification:
For each test case, print in one line two numbers: the number of different shortest paths between C​1 and C2 , and the maximum amount of rescue teams you can possibly gather. All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

Sample Input:
5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1
Sample Output:
2 4

具体要求

题就自己百度翻译吧,就是迪杰斯特拉算法,同时添加的顶点的数值,输出也是输出最短路径的条数,略微变形就可以了。

具体代码

#include<stdio.h>
#include<stdlib.h>
#define MAX 500
#define BKD 65535
#define ERROR -1
#define false -1
#define true 1
typedef struct node *Post;
struct node
{
    int ding;
    int bian;
    int m[MAX][MAX];
};/*邻接矩阵储存图*/
typedef Post Mgraph;
int qi,dao;/*起始点*/
int dist[MAX];/*Dijkstra算法的辅助数组*/
int cist[MAX];/*也是辅助数组,主要针对路的收费*/
int path[MAX];/*回溯数组*/
Mgraph JIANtu()
{
    Mgraph M;
    M=(Mgraph)malloc(sizeof(struct node));
    int b,d,i,j,zh,a;
    scanf("%d",&M->ding);/*先接受顶点数*/
    for( i=0;i<M->ding;i++)
        for(j=0;j<M->ding;j++)
            M->m[i][j]=BKD;/*初始化矩阵,值要为一个较大的数*/
    scanf("%d %d %d",&M->bian,&qi,&dao);
    for(i=0;i<M->ding;i++)
        scanf("%d",&cist[i]);
    for(i=0;i<M->bian;i++)
    {
        scanf("%d %d %d",&b,&d,&zh);/*输入,无向图*/
        M->m[b][d]=zh;
        M->m[d][b]=zh;
    }
    return M;
}
int Findmin(Mgraph M,int dist[],int co[])/*实现Dijkstra算法中的找出,dist值最小的那一个*/
{
    int mv,i;
    int md=BKD;
    for(i=0;i<M->ding;i++)
    {
        if(co[i]==false&&dist[i]<md)
        {
            md=dist[i];
            mv=i;
        }
    }
    if(md<BKD)
    {
        return mv;/*如果没有了返回error*/
    }
    else
        return ERROR;
}
int SUM(int a)/*回溯根据回溯数组,加出救援队的值*/
{
    int i=a;
    int sum=0;
     while(path[i]!=-1)
    {
        sum=sum+cist[i];
        i=path[i];
    }
    sum+=cist[qi];
    return sum;
}
void Dijkstra(Mgraph M)/*加入一些东西的迪杰斯特拉算法*/
{
    int co[MAX];/*标记数组*/
    int biaoji[MAX];/*用来储存,每个顶点的最短路径数*/
    int i,j,sum,cheng,cheng1;
    int b1,b2;
    sum=0;
    for(i=0;i<M->ding;i++)/*初始化*/
    {
        dist[i]=M->m[qi][i];
        co[i]=false;
        if(dist[i]<BKD)
        {
            path[i]=qi;
            biaoji[i]=1;/*有边链接,最短路径就为1啊*/
        }
        else
        {
            path[i]=-1;/*没有就为0啊*/
            biaoji[i]=0;
        }
    }
    dist[qi]=0;/*标记初始啊*/
    biaoji[qi]=1;
    co[qi]=true;/*起点入表*/
    while(1)
    {
        i=Findmin(M,dist,co);
        if(i==ERROR)
              break;
        co[i]=true;
        if(biaoji[i]==0)
            biaoji[i]=1;/*如果找出了一个点距离小于BKD,肯定有路径,所以这个点的biaoji[i]==0时,就更新为1*/
        for(j=0;j<M->ding;j++)
        {
            if(co[j]==false&&M->m[i][j]<BKD)/*没被标记且有边相连*/
            {
                if(biaoji[j]==0)/*有边相连,且biaoji[i]==0,就更新为此点的最短路径数为1*/
                    biaoji[j]=1;
                if(dist[i]+M->m[i][j]<dist[j])
                {
                    dist[j]=dist[i]+M->m[i][j];
                    path[j]=i;
                    biaoji[j]=biaoji[i];/*找到一条更短的路,所以最短路径数也要继承过去*/
                }
                else if(dist[i]+M->m[i][j]==dist[j])
                {
                    if(biaoji[j]==1)
                    {
                         biaoji[j]=biaoji[i]+biaoji[path[i]];/*biaoji[j]==1代表第一次出现相等,所以他的最短路径数,等于新路径的点的最短路径数加上旧路径上的点的最短路径数,因为此时biaoji[j]=1*/
                    }
                    else
                    {

                        biaoji[j]=biaoji[j]+biaoji[i];/*第二次相等,或者第n次相等,此时这个点的最短路径数就本身的加上新路径的*/

                    }
                    b1=SUM(i);
                    b2=SUM(j)-cist[j];/*回溯函数,比较新旧路径上,谁的救援队多*/
                    if(b2<b1)
                    {
                       path[j]=i;/*如果新的路径上多,更新回溯点*/
                    }
                }
            }
        }
    }
    /*for(i=0;i<M->ding;i++)
        printf("%d ",biaoji[i]);*/
    sum=SUM(dao);/*回溯计算救援队的值*/
    printf("%d %d",biaoji[dao],sum);/*输出两个的值*/
}
int main()
{
    Mgraph M;
    M=JIANtu();
    Dijkstra(M);/*调用函数,结束程序*/
    return 0;
}



/*8 11 0 6
1 1 1 1 10 1 1 10
0 1 1
0 2 1
0 3 1
0 4 1
0 7 1
1 5 1
2 5 1
3 5 1
4 6 2
5 6 1
7 6 2*/

具体思路

代码后面跟了一组测试数据,正确答案应该是5 12,自己设计了很多组测试数据,但是就记录这一组,可以使用。

结果

在这里插入图片描述
代码很多不足,望指出一起进步,谢谢!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值