P1807 最长路 拓扑排序/bfs

 P1807 最长路 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

题目很简单,求1-n的最长路。但是题目中含有负权,不能将dijkstra的松弛反着使用。考虑拓扑排序,从1开始不断pop直到n,期间不断用松弛操作更新最长路径。但是要注意。图中入度为0的点可能不止一个,那么如果从1开始topo,则有些点因为始终有个父节点而无法被遍历到。所以初始要把所有入度为0 的都加入队列。然后在每次更新子节点的时候对各个顶点松弛去最大,这样就可以得到每个顶点的最长路。最后输出dis[n]即可

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define mm(a) memset(a,0,sizeof(a))
#pragma warning (disable :4996);
const int N = 2e5 + 10;
int T, n, m, k, q;
int a, b, c;
struct edge {
	int to, pw;
};
vector<edge>gra[N];
int vis[N], dis[N];
int deg[N];
void topo() {
	queue<int>q;
	int sum = 0;
	for (int j = 2; j <= n; j++) {
		dis[j] = INT_MIN;
		if (deg[j] == 0)
			q.push(j);
	}
	q.push(1);
	while (!q.empty()) {
		int now = q.front();
		q.pop();
		sum++;
		for (edge x : gra[now]) {
			deg[x.to]--;
			dis[x.to] = max(dis[x.to], dis[now] + x.pw);
			if (!deg[x.to])
				q.push(x.to);
		}
	}
	int ans = 0;
	if (dis[n]!=INT_MIN)
		cout << dis[n] << endl;
	else
		cout << -1;
}
signed main() {
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	//freopen("out.txt", "w", stdout);
#endif
	cin >> n >> m;
	for (int j = 1; j <= m; j++) {
		cin >> a >> b >> c;
		gra[a].push_back({ b,c });
		deg[b]++;
	}
	topo();
}

但是真的需要这么麻烦吗?这里用拓扑的目的 也就是为了遍历每个顶点,同时更新最长路径。那么我们其实用bfs也能做到,所以我们尝试以1为起点去遍历每个顶点,同时维护dis数组(表示1到下标的最长路)有点类似dijkstra算法,但是避开了dijkstra的拓展操作,从而可以处理负权图,但是复杂度会大一些,因为bfs是没有目的不断更新最长路的

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define mm(a) memset(a,0,sizeof(a))
#pragma warning (disable :4996);
const int N = 2e5 + 10;
int T, n, m, k, q;
int a, b, c;
struct edge {
	int to, pw;
};
vector<edge>gra[N];
int vis[N], dis[N];

void bfs() {
	queue<edge>q;
	int ans = INT_MIN;;
	for (int j = 1; j <= n; j++)dis[j] = INT_MIN;
	int now = 1;
	dis[now] = 0;
	q.push({ now,0 });//从1出发
	while (!q.empty()) {
		now = q.front().to;
		q.pop();
		if(n==now)ans = max(ans, dis[now]);
		else
		for (auto x : gra[now]) {
			int to = x.to;
			if (dis[to] < dis[now] + x.pw) {
				dis[to] = dis[now] + x.pw;//不断松弛直到最长
				q.push(x);
			}
		}
	}
		if (ans == INT_MIN)
			cout << -1 << endl;
		else
		cout << ans<< endl;
}

signed main() {
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	//freopen("out.txt", "w", stdout);
#endif
	cin >> n >> m;
	for (int j = 1; j <= m; j++) {
		cin >> a >> b >> c;
		gra[a].push_back({ b,c });
	}
	bfs();

}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
P1807 最长是一道洛谷的题目,要求计算一个有向无环图中1到n之间的最长径。关于这个问题,可以使用广搜算法来依次搜索图中的结点,并记录当前结点的最长并不断更新。具体的实现可以使用邻接矩阵来存储边权,并注意可能存在重边的情况需要取最大值。此外,为了避免超时,可以在入队时加上一个判断语句,只有当到达该点的最长需要更新时才进行入队操作。以下是一种可能的解法: ```cpp #include <bits/stdc++.h> #define endl '\n' #define inf 0x3f3f3f3f using namespace std; typedef long long ll; const ll mo=80112002; int n, m, u, v, ww, w = max(w[u][v], ww); } q.push(1); memset(a, -1, sizeof(a)); a = 0; while (!q.empty()) { int x = q.front(); q.pop(); for (int i=1; i<=n; i++) { if (w[x][i && a[i < a[x + w[x][i]) { a[i = a[x + w[x][i]; q.push(i); } } } cout << a[n]; return 0; } ``` 以上是一种可能的代码实现,使用了广搜算法来计算最长径。该算法使用邻接矩阵存储边权,通过不断更新当前结点的最长来得到1到n之间的最长径。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [洛谷P1807 最长BFS)](https://blog.csdn.net/m0_60543948/article/details/126431087)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [图论——最长(洛谷 P1807)](https://blog.csdn.net/weixin_44572229/article/details/120743825)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值