本文主要是介绍【PAT】【Advanced Level】1003. Emergency (25),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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
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.
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 1Sample Output
2 4
原题链接:
https://www.patest.cn/contests/pat-a-practise/1003
数据量很小,DFS可通过
坑点:
让输出最短路的个数,而不是长度
教训:
最短路算法还应熟悉
认真读题
针对小数据量,直接暴力
CODE:
#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
#define N 500
#define inf 10000000
using namespace std;
bool visited[N];
int dist[N][N];
int ps[N];int m,n;
int sta,des;
int val=0;
int vc=0;
int minr=inf;
int maxx=-1;
int numm=0;
void dfs(int s)
{if (s==des){if (vc<minr){numm=1;minr=vc;maxx=val;}else if(vc==minr){numm++;maxx=max(maxx,val);}return;}for (int i=0;i<n;i++)if (dist[s][i]!=inf && visited[i]!=1 && i!=s){val+=ps[i];vc+=dist[s][i];visited[i]=1;if (vc<=minr)dfs(i);visited[i]=0;val-=ps[i];vc-=dist[s][i];}return;
}int main()
{memset(visited,0,sizeof(visited));cin>>n>>m;cin>>sta>>des;for (int i=0;i<n;i++){cin>>ps[i];for (int j=0;j<n;j++)dist[i][j]=inf;}for (int i=0;i<m;i++){int a,b,c;cin>>a>>b>>c;dist[a][b]=min(c,dist[a][b]);dist[b][a]=min(c,dist[b][a]);}val+=ps[sta];dfs(sta);cout<<numm<<" "<<maxx;return 0;
}
这篇关于【PAT】【Advanced Level】1003. Emergency (25)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!