都是答案错误,确实找不出错在哪里
原题连接:https://pintia.cn/problem-sets/994805342720868352/problems/994805379664297984
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
vector<int>citynode[1024];
int cityh[1024];
int totalcityh[1024];
int vist[1024];
int dcost[1024];
int citycost[1024][1024];
int pre[1024],rountnum[1024];
map<string, int>city2n;
map<int, string>n2city;
int num = 0;
int fcity;
const int INF = 100000000;
int placenum[1024];
void citytonum(char city[30]) {
if (city2n.find(city) == city2n.end()) {
city2n[city] = num;
n2city[num] = city;
num++;
}
return ;
}
//int countnode(int oo,int noden) {
// int preo = pre[oo];
// if (oo == fcity)
// {
// return noden;
// }
// countnode(preo, noden+1);
//
//}
void djst(int scity) {
//初始化
fill(dcost, dcost+1024,INF); //边权
dcost[scity] = 0; //
totalcityh[scity] = cityh[scity]; //增加一个点权
rountnum[scity] = 1; //路径数
////遍历每个城市
for (int i = 0; i < num; i++) {
//找到最小距离的城市
int temp = -1, mincost = INF;
for (int j = 0; j < num; j++) {
if (vist[j] == 0 && dcost[j] < mincost) {
temp = j;
mincost = dcost[j];
}
}
if (temp == -1) {
return;
}
vist[temp] = 1;
for (int m = 0; m < citynode[temp].size(); m++) {
int v = citynode[temp][m];
if (vist[v] == 0 && dcost[v] > citycost[temp][v] + dcost[temp]) {
dcost[v] = citycost[temp][v] + dcost[temp];
totalcityh[v] = totalcityh[temp]+ cityh[v];
rountnum[v] = rountnum[temp];
placenum[v] = placenum[temp]+1;
pre[v] = temp;
}
else if (vist[v] == 0 && dcost[v] == citycost[temp][v] + dcost[temp]) {
if (cityh[v] + totalcityh[temp]> totalcityh[v]) {
totalcityh[v] = totalcityh[temp]+ cityh[v];
placenum[v] = placenum[temp] + 1;
pre[v] = temp;
}
else if (cityh[v] + totalcityh[temp]== totalcityh[v] && placenum[v] > placenum[temp] + 1) {
pre[v] = temp;
placenum[v] = placenum[temp] + 1;
}
rountnum[v] += rountnum[temp];
}
}
}
}
vector<int>rescity;
void dfs(int node) {
int precity = pre[node];
if (node == fcity)
{
rescity.push_back(node);
return;
}
dfs(precity);
rescity.push_back(node);
}
int main()
{
#ifndef ONLINE_DUBGE
freopen("1.txt", "r", stdin);
#else
#endif
int N, M;
char startcity[30];
fill(citycost[0], citycost[0] + 1024 * 1024, INF); //先把起点到各个点的距离变成最大
fill(vist, vist + 1024, 0);
citytonum("HZH");
scanf("%d %d %s", &N, &M, &startcity);
for (int i = 0; i < N-1; i++) {
int happ;
char city[30];
scanf("%s %d", &city,&happ);
citytonum(city);
cityh[city2n[city]] = happ;
}
fcity = city2n[startcity];
for (int i = 0; i < M; i++) {
int cost;
char city1[30];
char city2[30];
scanf("%s %s %d", &city1, &city2,&cost);
citycost[city2n[city1]][city2n[city2]] = cost;
citycost[city2n[city2]][city2n[city1]] = cost;
citynode[city2n[city1]].push_back(city2n[city2]);
citynode[city2n[city2]].push_back(city2n[city1]);
}
djst(fcity);
int des = city2n["ROM"];
dfs(des);
//int aaa = countnode(des, 1);
printf("%d %d %d %d\n", rountnum[des],dcost[des], totalcityh[des], totalcityh[des] / (placenum[des]));
for (int i = 0; i < rescity.size(); i++) {
if (i == rescity.size() - 1) {
printf("%s", n2city[rescity[i]].c_str());
}
else {
printf("%s->", n2city[rescity[i]].c_str());
}
}
return 0;
}