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
自从退役后实在是好久没有敲题,感觉代码能力、数据结构、算法都忘记了[T T]
这题求最短路径,可以用Dijkstra,也可以用DFS,但输出确是:the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather. 【坑点1】
而且,测试数据有C1==C2的情况,wa了第二个数据很久很久= =【坑点2】
上代码:
#include
#include
#include
#include
using namespace std;
const int maxnum = 505;
const int maxint = 0x3f3f3f3f;
int N,M,C1,C2;
void Dijkstra(int n, int v, int *dist, int *h,int *prev, int c[maxnum][maxnum],int hands[maxnum],int *pathcount)
{
bool s[maxnum]; // 判断是否已存入该点到S集合中
int i;
for(i=0; ih[j])
h[j] = newh;
}
}
}
}
int main()
{
int dist[maxnum];
int prev[maxnum];
int h[maxnum];
int pathcount[maxnum];//用来记录所有最短路径的条数
int hands[maxnum];
int A[maxnum][maxnum];
memset(hands,0,sizeof(hands));
memset(A,0,sizeof(A));
while(~scanf("%d%d%d%d",&N,&M,&C1,&C2))
{
int i,j;
for(i=0;i
终于来贴DFS的代码了,看着大牛的打完,感觉吧,看完代码能理解,但是搁自己就敲不出DFS,天哪,谁来教教我!!
稍微说说对DFS的想法,首先要明确返回的条件是什么(这里是等于终点的时候),其次,如果有位置标记的话,返回的什么时候取消标记(这里是那个s数组,只是对于取消 标记是在循环外我有点蒙蔽,有人教教我就好了!先谢谢您!)
上代码:
#include
#include
#include
#include
using namespace std;
const int maxnum = 505;
const int maxint = 0x3f3f3f3f;
int N,M,C1,C2;
int A[maxnum][maxnum];
int hands[maxnum];
int mindist = maxint;
int maxhand = 0;
int cnt = 0;
int s[maxnum];
void dfs(int v,int dist,int hand)
{
if(v==C2)
{
if(distmaxhand)
maxhand = hand;
}
return;
}
s[v]=1;
int i,j;
for(i=0;i0)
dfs(i,dist+A[v][i],hand+hands[i]);
}
s[v]=0;
}
int main()
{
while(~scanf("%d%d%d%d",&N,&M,&C1,&C2))
{
int i,j;
memset(s,0,sizeof(s));
for(i=0;i