萌新求救
  • 板块P1396 营救
  • 楼主Jur_Cai
  • 当前回复1
  • 已保存回复1
  • 发布时间2020/8/16 22:30
  • 上次更新2023/11/6 20:05:31
查看原帖
萌新求救
105865
Jur_Cai楼主2020/8/16 22:30
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#define INF 0x3f3f3f3f
using namespace std;
struct Edge {
	int to,next,dis;
} e[20005];
int head[20005],tot=0;
inline void add(int u,int v,int w) {
	e[tot].to=v;
	e[tot].next=head[u];
	e[tot].dis=w;
	head[u]=tot;
	tot++;
}
int dis[10005];
bool iq[10005];
queue<int> q;
int main() {
	memset(head,-1,sizeof(head));
	int n,m,s,t;
	scanf("%d%d%d%d",&n,&m,&s,&t);
	for(int i=1; i<=m; i++) {
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		add(u,v,w);
		add(v,u,w);
	}
	memset(dis,INF,sizeof(dis));
	dis[s]=0;
	q.push(s);
	iq[s]=1;
	if(!q.empty()) {
		int x=q.front();
		q.pop();
		iq[x]=0;
		for(int i=head[x]; i!=-1; i=e[i].next)
			if(dis[e[i].to]>dis[x]+e[i].dis) {
				dis[e[i].to]=dis[x]+e[i].dis;
				if(!iq[e[i].to]) {
					iq[e[i].to]=1;
					q.push(e[i].to);
				}
			}
	}
	printf("%d",dis[t]);
	return 0;
}

不知道为什么queue好像抽风了QwQ

2020/8/16 22:30
加载中...