不入度直接从1开始bfs为什么不对
  • 板块P1807 最长路
  • 楼主Cangshu
  • 当前回复1
  • 已保存回复1
  • 发布时间2021/11/23 23:50
  • 上次更新2023/11/3 23:40:07
查看原帖
不入度直接从1开始bfs为什么不对
496340
Cangshu楼主2021/11/23 23:50
#include <bits/stdc++.h>
using namespace std;
int n, m, u, v, l, ans, f[50005], ind[50005]; 
struct edge {
	int to, cost;
};

vector<edge> p[50005];
queue<int> q;

int main () {
	cin >> n >> m;
	f[n] = -1;
	for (int i = 1; i <= m; i++) {
		int bol = 0;
		cin >> u >> v >> l;
		for (int i = 0; i < p[u].size(); i++) {    //处理重边,只取权最大的
			if(p[u][i].to == v) {
				p[u][i].cost = max(p[u][i].cost, l);
				bol = 1;
			}
		}
		if(bol == 0) p[u].push_back((edge) {v, l});
	}
	q.push(1);
	while (!q.empty()) {
		int x = q.front();
		q.pop();
		for (int i = 0; i < p[x].size(); i++) {
			int y = p[x][i].to;
			f[y] = max(f[y], f[x] + p[x][i].cost);
//			ind[y]--;
//			if (ind[y] == 0) 
			q.push(y);
		}
	}
	cout << f[n];
	return 0;
}

WA了5, 6两个点

2021/11/23 23:50
加载中...