题目要求:找到到达目标的最短路径条数,并计算能到达目标点最多的人数!!
#include<iostream>
#include<stdio.h>
using namespace std;
const int maxN=505;
const int bignum=999999;
class node
{
public:
int to,next,cost;
};
class st
{
public:
int cost,man,way;
};
node edge[maxN*3];
st ans[maxN];
int head[maxN],cnt=0,team[maxN];
int N,M,C1,C2;
void Init()
{
int i,a,b,c;
scanf("%d%d%d%d",&N,&M,&C1,&C2);
for(i=0;i<maxN;i++) head[i]=0;
for(i=0;i<N;i++)
scanf("%d",&team[i]);
for(i=0;i<M;i++)
{
scanf("%d%d%d",&a,&b,&c);
edge[++cnt].to =b;
edge[cnt].next =head[a];
edge[cnt].cost =c;
head[a]=cnt;
edge[++cnt].to =a;
edge[cnt].next =head[b];
edge[cnt].cost =c;
head[b]=cnt;
}
}
int vis[maxN];
void dfs(int u)
{
int i;
for(i=head[u];i;i=edge[i].next ){ // 遍历
int v=edge[i].to,now_cost ;
if(!vis[v]){
now_cost=ans[u].cost +edge[i].cost ;
if(now_cost<ans[v].cost ){ // 找到新的路径
ans[v].cost =now_cost;
ans[v].way =ans[u].way; //
ans[v].man =(ans[u].man +team[v]);
}
else if(now_cost==ans[v].cost ){ // 相等的时候 路径+1
ans[v].way +=1;
ans[v].man =ans[v].man >(ans[u].man +team[v])?ans[v].man :(ans[u].man +team[v]) ;
}
else if(now_cost>ans[v].cost) // 路径超过之前的,不在往下
continue;
if(v==C2) continue ; //到达目标
vis[v]=1;
dfs(v);// 继续往下深搜~
vis[v]=0;
}
}
}
void solve()
{
int i,j;
for(j=0;j<maxN;j++) vis[j]=0;
for(i=0;i<maxN;i++) {
ans[i].cost =bignum; ans[i].way =0;
}
ans[C1].way=1;ans[C1].cost =0; ans[C1].man=team[C1];vis[C1]=1;
dfs(C1);
printf("%d %d\n",ans[C2].way,ans[C2].man);
}
int main()
{
Init();
solve();
return 0;
}