码迷,mamicode.com
首页 > 其他好文 > 详细

pat 1003

时间:2019-12-30 23:11:43      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:must   -o   numbers   start   nts   diff   pac   imu   include   

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 (≤) - the number of cities (and the cities are numbered from 0 to N1), M - the number of roads, C?1?? and C?2?? - 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 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


看到题第一反应是迪杰斯特拉和floyd,想了很久,但是好像不太行。直接用深搜,记录下救援人数和路径就行。正好练习一下链表法存储图
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define ll long long
#define Max 505
#define _for(i, a, b) for (int i = (a); i <= (b);i++)
struct edge{
    int w, v, next;
    edge(int v=0,int w=0,int next=0):v(v),w(w),next(next){}
} e[Max << 1];
int head[Max],n,m,st,de,point[Max],Size=0;
bool vis[Max];      //防止形成环路
int ans = -1,cnt=-1;
int zdlj = 0x3f3f3f3f;//最短路径
void insert(int u,int v,int w){
    e[Size] = edge(v, w, head[u]);
    head[u] = Size++;
}
void insert2(int u,int v,int w){
    insert(u, v, w);
    insert(v, u, w);
}
void dfs(int u,int road_w, int road_ans){      //当前所在的点,当前走过的距离,救援人数
    if(u==de){
        if(road_w<zdlj){      //最短路径更新
            zdlj = road_w;
            cnt = 1;       //找到了另一条最短路,重新计数
            ans = road_ans;   //最大救援人数更新
        }else if(road_w==zdlj){
            cnt++;   //长度相同,cnt+1
            ans = max(ans, road_ans);  //更新最大救援人数
        }
        return;
    }
    for (int i = head[u]; ~i;i=e[i].next){
        int _v = e[i].v, _w = e[i].w;
        if(!vis[_v]){
            vis[_v] = true;
            dfs(_v, road_w + _w, road_ans + point[_v]);
            vis[_v] = false;       //回溯。之前走过的点可以再走一次
        }
    }
}
int main()
{
    memset(vis,false,sizeof(vis));
    memset(head, -1, sizeof(head));
    cin >> n >> m >> st >> de;
    _for(i,0,n-1){
        cin >> point[i];
    }
    while(m--){
        int u, v, w;
        scanf("%d%d%d", &u, &v, &w);
        insert2(u, v, w);
    }
    vis[st] = true;
    dfs(st, 0, point[st]);
    cout << cnt << " " << ans << endl;
    return 0;
}


pat 1003

标签:must   -o   numbers   start   nts   diff   pac   imu   include   

原文地址:https://www.cnblogs.com/zlwjy/p/12121950.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!