Dij板子求助
  • 板块P1342 请柬
  • 楼主QZY2008
  • 当前回复2
  • 已保存回复2
  • 发布时间2021/10/22 22:17
  • 上次更新2023/11/4 02:47:23
查看原帖
Dij板子求助
547609
QZY2008楼主2021/10/22 22:17

萌新刚学OI1秒钟。望大佬帮助

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=1e6+1;
struct Node{
	ll to,nxt;
	ll value;
};
Node edge[N<<2];
ll head[N<<2],tot=0;
ll dis[N<<2];
struct node{
	ll fa;
	ll dis;
	bool operator <(const node x)const{
		return x.dis<dis;
	}
};
inline void add_edge(ll u,ll v,ll value){
	++tot;
	edge[tot].nxt=head[u];
	edge[tot].value=value;
	edge[tot].to=v;
	head[u]=tot;
}
ll n,m,s,t;
bool vis[N<<2];
priority_queue<node>Q;
inline ll read(){
	ll x=0,f=1;
	char ch=getchar();
	for (;!isdigit(ch);ch=getchar())
		if (x=='-')f=-1;
	for (;isdigit(ch);ch=getchar())
		x=(x<<1)+(x<<3)+(ch^48);
	return x*f;
}
inline void dijkstra(){
	dis[s]=0; Q.push((node){0,s});
	while (!Q.empty()){
		node tmp=Q.top();
		Q.pop();
		ll cnt=tmp.fa;
		if (vis[cnt])continue;
		vis[cnt]=1;
		for (ll i=head[cnt];i;i=edge[i].nxt){
			ll res=edge[i].to;
			if (dis[res]>dis[cnt]+edge[i].value){
				dis[res]=dis[cnt]+edge[i].value;
				if(!vis[res])
                    Q.push((node){dis[res],res});
			}
		} 
	}
}
int main(){
	n=read(),m=read(),s=1,t=n;
	for (ll _=1;_<=n;_++)
		dis[_]=INT_MAX;
	for (ll _=1;_<=m;_++){
		ll u=read(),v=read(),val=read();
		add_edge(u,v,val);
	}
	dijkstra();
	printf("%lld\n",dis[t]);
}
2021/10/22 22:17
加载中...