为什么用 dijikstra 能过?
查看原帖
为什么用 dijikstra 能过?
1625451
Tommy_Shelby_楼主2025/6/26 19:49
#include <bits/stdc++.h> 
using namespace std;
#define int long long
#define inf 0x3f3f3f3f
#define llf 0x3f3f3f3f3f3f3f3f
#define pll pair<int,int>
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n,m;
    cin>>n>>m;
    vector<vector<pll>> a(n+1);
    vector<int> dis(n+1,-llf);
    priority_queue<pll> q;
    for(int i=1;i<=m;i++){
    	int u,v,w;
    	cin>>u>>v>>w;
    	a[u].emplace_back(v,w);
	}
	dis[1]=0;
	dis[n]=-1;
	q.emplace(0,1);
	while(!q.empty()){
		int dx=q.top().first,x=q.top().second;
		q.pop();
		if(dx<dis[x]) continue;
		for(int i=0;i<a[x].size();i++){
			int w=a[x][i].second,y=a[x][i].first;
			if(dx+w>dis[y]){
				dis[y]=dx+w;
				q.emplace(dis[y],y);
			}
		}
	}
	cout<<dis[n];
	return 0;
}
2025/6/26 19:49
加载中...