1003-Emergency

无人久伴 提交于 2020-03-01 12:59:19
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 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 Specification:
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  
作者: CHEN, Yue
单位: 浙江大学
时间限制: 400 ms
内存限制: 64 MB

思路提点:
  考虑DFS,从指定城市c1c_1出发,依次访问邻接城市,直到目标城市c2c_2。用LLLL记录到达目标城市最短路径长度,LL记录当前长度;gathergather记录最短路径最多的援救队,gtgt记录当前路径援救队,shortest记录最短路径条数。每次访问到目标城市就对LL,gather,shortestLL,gather,shortest进行更新。之后进行回溯继续搜索。C++代码如下:

#include<iostream>
#include<array>
using namespace std;

const int INF=0x3fffffff;
array<array<int,507>,507> pic;
array<int,503> team;
array<int, 507> vis;

// 结果
int shortest,gather=0;
// DFS信息
int LL=INF;	
int L=0;
int gt;

void DFS(int v, int d, int n)
{
    if(v == d)
    {
        if(L < LL)		// 找到比之前路径更短路径,最短路径条数置1,更新最短路径长度
        {				// 更新最多搜救队数目
            shortest = 1;
            LL = L;
            gather = gt;
        }
        else if(L == LL)	// 不止一条最短路径,更新最多搜救队数目
        {
            shortest++;
            gather=max(gather, gt);
        }
        return;				// 返回
    }

    vis[v]=1;   //置访问标记
    for(int i=0; i<n; ++i)
    {
        if(pic[v][i]!=INF && vis[i] == 0)
        {
            L += pic[v][i];
            gt += team[i];

            DFS(i, d, n);

            gt -= team[i];
            L -= pic[v][i];
        }
    }
    vis[v]=0;   // 回溯取消标记
}
int main()
{
    ios::sync_with_stdio(0);

    int n, m, c1, c2;
    cin>>n>>m>>c1>>c2;
    for(int i=0; i<n; ++i)
        cin>>team[i];

    for(int i=0; i<n; ++i)
        pic[i].fill(INF);

    int t1,t2,s;
    for(int i=0; i<m; ++i)
    {
        cin>>t1>>t2>>s;
        pic[t1][t2]=pic[t2][t1]=s;	// 无向图!
    }

    vis.fill(0);
    gt = team[c1];			// 源城市援救队数目

    DFS(c1,c2,n);

    cout<<shortest<<' '<<gather<<endl;
    return 0 ;
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!