题目
思路
没有用网上其他人写的dijkstra方法 采用了 dfs 算法
code
#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;
// 每个城市对应物资 城市之间举例矩阵
int goods[501], len[600][600] = {-1};
// 城市数 道路数 当前位置 救援位置 最小路径数 最大补给数 最小长度
int cityn,roadn,curr,save,roadsum = 0,maxGoods=-1,minlen=1000000000;
bool zou[501] = {false};
void findWay(int currg,int rn,int x,int dist){
if(x == save){
if(dist<minlen){// 找到新的最小值时
minlen = dist;
roadsum = 1;
maxGoods = currg;
}else if(dist == minlen){
roadsum++;
maxGoods = max(currg,maxGoods);
}
return;
}
if(rn>roadn||dist>minlen) return;
for (int i = 0; i < roadn; i++)
{
if(i==x||zou[i]||len[x][i]<=0) continue;
zou[i] = true;
findWay(currg+goods[i],rn+1,i,dist+len[x][i]);
zou[i] = false;
}
}
int main(){
cin>>cityn>>roadn>>curr>>save;
for (int i = 0; i < cityn; i++)
{
cin>>goods[i];
}
for (int i = 0; i < roadn; i++)
{
int a,b,lens;
cin>>a>>b>>lens;
len[a][b] = lens;
len[b][a] = lens;
}
findWay(goods[curr],1,curr,0);
cout<<roadsum<<" "<<maxGoods<<endl;
}