1003 Emergency(25 分)

每次写正文之前都是鬼鬼珊的闲聊一分钟,哈哈。

最近准备九推的事,PAT的练习被搁浅了,好吧,我可能是为自己找了个借口。本来昨天回到学校,想在晚上睡觉一定要写一篇,可是,最开始和薛老师吃饭,聊了各自联系的导师,哈哈,是不是学计算机,都在焦虑将来会不会秃顶。上学期期末考试的那段时间,感觉自己的头发也掉得特别厉害。然后和杨同学去操场闲逛,她和她男朋友的故事让我觉得很奇幻,就像小说里面会发生的事。虽然没有谈过恋爱,但是爱情里面应该有点崇拜的色彩才会更….(词穷的我实在找不到词来表达这个意思,或许是爱情的乐趣和苦趣),我想是这样的。

哈哈,还有一个特别特别棒的消息。我之前一直以为我的博客不会被别人看到,最近发生了两大很开心的事。第一重大突破,粉丝突破了0这个大关卡,虽然是我们班上的同学关注的,哈哈。第二个大突破,某个陌生道友评论了我的第一篇博客,感到很开心,就像是用了所学的知识,做了什么了不得的事。鬼鬼珊是不是应该发个福利呀……等粉丝突破一位数,嘿嘿

好吧,我切换频道了

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, C​1and 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 C​1 to C2.

Output Specification:
For each test case, print in one line two numbers: the number of different shortest paths between C​1 and C​2​​ , 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

题意:这是求图中两个顶点的之间的最短路径问题,但是可能最短路径不止一条,输出这个最短路径的条数。然后在众多最短路径中,选择中途能聚集的医疗团队个数最多的那条,输出聚集的医疗个数。

我在做这道题的时候,因为之前做过类似的题,所以并没有花多少时间。第一次做那道题的时候,花了很多很多时间,那道题比这道题要难,我很想写那个题,但是…….我还有点问题,需要再捣鼓捣鼓一下那个题(好吧,我承认还有的测试点我没有拿到分)。

注意点:

  1. 当然啦,两点之间的最短路径,我想到的就是Dijkstra算法,但是注意,这里的最短路径可能不止一条,所以肯定要对这个算法稍稍改进一下啦。而且对到达目的节点的每条最短路径的医疗队个数有一个记录,然后选择最大的即可。
  2. 到达每个节点的最短路径个数我们不确定,用vector这个数据结构。每个节点一个容器,存放到达当前站点的各个最短路径的总的医疗个数。每个容器的大小就是到达当前这个节点最短路径的条数。vector使用讲解

贴代码啦:

#include <iostream>
#include<vector>
#include<string.h>
using namespace std;
#define MaxSize 500
int Graph[MaxSize][MaxSize];//存放图
int vis[MaxSize]={0},dis[MaxSize];//存放访问标记和路径长度,并不需要存储路径
int curNum[MaxSize]={0};//存放当前的城市医疗队
int n,m,cur,des;//城市个数,m为路径,cur为目前所在城市,des为目标城市
const int INF=0x7fffffff;       //定义一个很大的数
//定义一个容器,存放到达当前站点的最多医疗数
vector<int> vec[MaxSize];
//输入函数
void Input(){
    cin>>n>>m>>cur>>des;
    int i;
    int x,y,z;
    memset(Graph, -1, sizeof(Graph));
    for(i=0;i<n;i++){
        dis[i]=MaxSize;
    }
    for(i=0;i<n;i++){
        cin>>curNum[i];
    }
    for(i=0;i<m;i++){
        cin>>x>>y>>z;
        Graph[x][y]=z;
        Graph[y][x]=z;
    }
}
void push(int target,int dataid)//将当前dataid统计的数据送到target
{
    for(int i=0;i<vec[dataid].size();i++){
        vec[target].push_back(vec[dataid][i]+curNum[target]);
    }
}
void Dij(int source,int destination){
    vis[source]=1;
    dis[source]=0;
    //将初始起点放进容器
    vec[source].push_back(curNum[source]);
    int i;
    int cen=source;
    int Min;
    while(1){
        //1、找到cen相邻的点并且未访问
        for(i=0;i<n;i++){
           if(vis[i]==0&&Graph[cen][i]!=-1){
              if(dis[cen]+Graph[cen][i]<dis[i]){
                 dis[i]=dis[cen]+Graph[cen][i];
                 vec[i].clear();//将对应的值清空
                 push(i,cen);
              }
              else if(dis[cen]+Graph[cen][i]==dis[i])//相等,说明是多条路径
              {
                  push(i,cen);
              }
           }
        }
        //2、找到下一个节点
        Min=INF;
        for(i=0;i<n;i++){
            if(vis[i]==0&&Min>dis[i]){
                cen=i;
                Min=dis[i];

            }
        }
        vis[cen]=1;
        if(cen==destination){
            break;
        }
    }
}
int main()
{
    Input();
    int i,j;
    if(n==0){
        cout<<"0 0"<<endl;
        return 0;
    }
    else if(cur==des){
            cout<<"1 "<<curNum[cur];
             return 0;
    }
    else{
        Dij(cur,des);
        vector<int> tt=vec[des];
        cout<<tt.size()<<' ';
        j=tt[0];
        for(i=1;i<tt.size();i++){
            if(j<tt[i]){
                j=tt[i];
            }
        }
        cout<<j;
    }
    return 0;
}


  • 12
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 24
    评论
评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值