这题#4、5、6运行错误,代码如下:
#include <cstdio>
#include <iostream>
using namespace std;
struct Edge {
int to, next, dis;
};
int n, m, ans = 0;
int head[50001], way[100000][2];
Edge edge[1501];
void Bfs(int);
void AddEdge(int, int, int, int);
int main() {
/*
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
*/
scanf("%d %d", &n, &m);
for (int i=1; i<=m; i++) {
int from, to, dis;
scanf("%d %d %d", &from, &to, &dis);
AddEdge(from, to, dis, i);
}
Bfs(1);
if (ans == 0) {
printf("-1\n");
} else {
printf("%d\n", ans);
}
/*
fclose(stdin);
fclose(stdout);
*/
return 0;
}
void Bfs(int start) {
int left = 0, right = 1;
way[1][0] = start;
do {
left++;
for (int i=head[way[left][0]]; i>0; i=edge[i].next) {
right++;
way[right][0] = edge[i].to;
way[right][1] = way[left][1]+edge[i].dis;
if (edge[i].to == n) {
ans = max(ans, way[right][1]);
}
}
} while (left < right);
}
void AddEdge(int from, int to, int dis, int i) {
edge[i].next = head[from];
edge[i].to = to;
edge[i].dis = dis;
head[from] = i;
}
dalao帮帮我。