admin管理员组文章数量:1577556
Emergency
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 (≤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~
这是PTA甲级的一道题。难度不大。但是有些细节困扰了我很久。所以特此写一篇纪念一下。
话不多说,这道题就是求两点之间满足路径最短的路的条数并且顶点值和最多。我则想着随便用个什么求最短路径,再在路径最短的基础上,深搜即可。于是就用了spfa求最短路径。
C++代码如下:
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
struct edge{
int v,next;
}e[300000];
int n,m,sta,End,cnt;
int node[510];
int s[300000];
int head[510];
int dis[510];
int book[510];
int Inf = 999999999;
queue<int> q;
int mins,maxw,count;
void add(int u,int v,int w)
{
cnt ++;
e[cnt].v = v;
e[cnt].next = head[u];
head[u] = cnt;
s[cnt] = w;
}
void spfa()
{
//切记队列不为空时循环
while(q.size())
{
int k = q.front();
book[k] = 0;
q.pop();
for(int i = head[k]; i; i = e[i].next)
{
int nextv = e[i].v;
//只有
if(dis[nextv] > dis[k] + s[i])
{
dis[nextv] = dis[k] + s[i];
if(book[nextv] == 0)
{
q.push(nextv);
book[nextv] = 1;
}
}
}
}
}
void dfs(int u, int w, int S)
{
if(u == End)
{
if(S == mins)
{
count ++;
maxw = max(maxw,w);
}
return;
}
for(int i = head[u]; i; i = e[i].next)
{
if(book[e[i].v] == 0)
{
int nextv = e[i].v;
book[nextv] = 1;
dfs(nextv,w + node[nextv],S + s[i]);
book[nextv] = 0;
}
}
}
int main()
{
scanf("%d%d%d%d",&n,&m,&sta,&End);
for(int i = 0; i < n; i ++)
scanf("%d",&node[i]);
for(int i = 1; i <= m; i ++)
{
int u,v,s;
scanf("%d%d%d",&u,&v,&s);
add(u,v,s);
add(v,u,s);
}
for(int i = 0; i < n; i ++)
dis[i] = Inf;
dis[sta] = 0;
//spfa求最短路径
book[sta] = 1;
q.push(sta);
spfa();
//求出最短路径再深搜
memset(book,0,sizeof(book));
book[sta] = 1;
mins = dis[End];
dfs(sta,node[sta],0);
//求得是最短路径的条数不是求最短路径
cout << count << " " << maxw;
return 0;
}
主要是以前没有注意的一点细节就是,spfa算法中一定要在松弛条件中判断是否入队列,并且每次出完队列后book数组要重新设置为0。
本文标签: Emergency
版权声明:本文标题:Emergency - C++ 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1727822572a1131899.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论