蒟蒻求助,马蜂好看
  • 板块学术版
  • 楼主Foofish
  • 当前回复6
  • 已保存回复6
  • 发布时间2021/4/10 20:27
  • 上次更新2023/11/5 00:44:25
查看原帖
蒟蒻求助,马蜂好看
203453
Foofish楼主2021/4/10 20:27

单源最短路,在谷上两个版本都能过,但是我们老师出同样的板子题(不过到不了输出-1),胡的数据就过不了。求大佬帮忙看看。 数据

die码如下

#include <bits/stdc++.h>
using namespace std;
template <typename T>inline void R(T& t){
    t=0; register char ch=getchar();
    while(!('0'<=ch&&ch<='9')){ if(ch=='-') t*=-1; ch=getchar(); }
    while(('0'<=ch&&ch<='9')){ t=((t<<1)+(t<<3))+ch-'0'; ch=getchar(); }
}
template <typename T,typename... Args> inline void R(T& t, Args&... args){R(t);R(args...);}
template <typename T>inline void W(T x){
    if(x<0) putchar('-'),x=~(x-1); int s[30],top=0; while(x) s[++top]=x%10,x/=10;
    if(!top) s[++top]=0; while(top) putchar(s[top--]+'0');
}
int n,m,s;
int dis[300086];
bool book[300086];
struct Node{
	int y,w;
	bool operator < (const Node &Poss)const{
		return w>Poss.w;
	}
};
priority_queue<Node>q;
vector<pair<int,int> >a[300086];
int main(){
//	freopen("1.in","r",stdin);
//	freopen("1.ans","w",stdout);
	cin>>n>>m>>s;
	for(int i=1;i<=m;++i){
		int f,t,we;
		R(f,t,we);
		a[f].push_back(make_pair(t,we));
	}
	memset(dis,0x3f3f3f3f,sizeof(dis));
	dis[s]=0;
	q.push({s,0});
	for(int i=1;i<=n;++i){
		while(!q.empty()&&book[q.top().y]) q.pop();
		if(q.empty()) break;
		int u=q.top().y;
		book[u]=1;
		for(unsigned j=0;j<a[u].size();++j)
			if(!book[a[u][j].first]&&dis[a[u][j].first]>dis[u]+a[u][j].second){
				dis[a[u][j].first]=dis[u]+a[u][j].second;
				q.push({a[u][j].first,dis[u]+a[u][j].second});
			}
	}
	for(int i=1;i<=n;++i)
		if(dis[i]==0x3f3f3f3f) cout<<-1<<' ';
		else cout<<dis[i]<<' '; 
    return 0;
}
2021/4/10 20:27
加载中...