PAT 1003. Emergency C语言

本文介绍了一个基于Dijkstra算法的城市间紧急救援路线规划问题。输入包括城市数量、道路连接及长度等信息,目标是最短时间到达指定城市并尽可能多地召集救援队伍。

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

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 c1, c2 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 C1 to C2.

Output

For each test case, print in one line two numbers: the number of different shortest paths between C1 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

单源最短路径 Dijkstra算法  

#include <stdio.h>
#define maxint 1<<28
int main(void){
    int n,m,c1,c2;
    scanf("%d%d%d%d",&n,&m,&c1,&c2);
    int i,j;
    int t[510],a[510]={0};
    int d[510],w[510]={0},v[510]={0};
    for (i=0;i<n;i++){
        scanf("%d",&t[i]);
        d[i]=maxint;
    }
    int r[500][500];
    int x,y;
    for (i=0;i<n;i++){
        for (j=0;j<n;j++){
            r[i][j]=-1;
        }
    }
    for (i=0;i<m;i++){
        scanf("%d%d",&x,&y);
        scanf("%d",&r[x][y]);
        r[y][x]=r[x][y];
    }
    
    d[c1]=0;
    a[c1]=t[c1];
    w[c1]=1;
    
    for (i=0;i<n;i++){
        int minn=maxint,min;
        for (j=0;j<n;j++){
            if ((d[j]<minn)&&(v[j]==0)){
                min=j;
                minn=d[j];
            }
        }
        if (minn==maxint) break;
        v[min]=1;
        
        for (j=0;j<n;j++)
            if ((v[j]==0)&&(r[j][min]!=-1)){
                if (d[j]>d[min]+r[j][min]){
                    d[j]=d[min]+r[j][min];
                    w[j]=w[min];
                    a[j]=a[min]+t[j];
                }
                else if (d[j]==d[min]+r[j][min]){
                    w[j]+=w[min];
                    if (a[j]<a[min]+t[j]) a[j]=a[min]+t[j];
                }
                
            }
        if (min==c2) break;
    }

    printf("%d %d\n",w[c2],a[c2]);
}

### 关于PAT乙级1003题的C语言解法 对于PAT乙级1003题,即《我要通过》这一题目,在处理时可以采用较为直观的方法来满足其需求。此题的核心在于判断给定的一系列字符串是否符合特定条件,并据此输出结果。 #### 输入与输出解析 输入部分首先会提供一个正整数n表示待测字符串的数量,之后依次给出这些字符串,最后再给出另一个正整数k以及k个用于查询的ID[^4]。针对这样的输入结构,程序应当能够有效地读取并存储数据以便后续处理。 #### 解决方案概述 为了高效解决这个问题,可以通过构建两个数组分别记录每个字符出现次数和对应位置的状态标记。遍历所有字符串的过程中更新这两个辅助数组的信息。当遇到询问操作时,则依据预先准备好的状态信息快速作出响应[^2]。 #### 实现细节说明 具体来说,下面是一个简单的实现方式: ```c #include <stdio.h> #include <string.h> #define MAX_N 10 /* 定义最大可能的字符串数量 */ char str[MAX_N][101];/* 存储各个字符串 */ int main(){ int n; scanf("%d", &n); for(int i = 0; i < n; ++i){ scanf("%s", str[i]); } // 这里的逻辑可以根据具体的题目要求调整 return 0; } ``` 上述代码片段展示了如何接收输入的数据。然而,完整的解决方案还需要加入对单个字符串内部特性的分析过程,比如计算有效字符的比例等,这部分的具体实施取决于实际的任务定义。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值