蒟蒻求助,为啥这道题一定要判断重边
查看原帖
蒟蒻求助,为啥这道题一定要判断重边
388382
ScriptAccess楼主2024/5/28 23:45

下面代码爆零:

#include<bits/stdc++.h>
using namespace std;
const int N=3e5+10;
using ll=long long;
ll d[N],d1[N],d2[N],ans;
bool p[N];
struct node{
	ll v,w;
	bool operator < (const node &p) const{
		return w<p.w;
	}
};
vector<node> g[N];
priority_queue<node> q;
void dijkstra(int st,ll d[])
{
	d[st]=0;
	q.push({st,0});
	while(!q.empty())
	{
		node top=q.top();
		q.pop();
		int kx=top.v;
		int dis=top.w;
		if(p[kx]) continue;
		p[kx]=1;
		for(auto i:g[kx])
		{
			ll nex=i.v;
			if(i.w+dis<d[nex])
			{
				d[nex]=i.w+dis;
				q.push({nex,d[nex]});
			}
		}
	}
}
int main()
{
	memset(d1,0x3f,sizeof(d1));
	memset(d2,0x3f,sizeof(d2));
	int n,m,u,v,w,s;
	cin>>n>>m;
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d%d",&u,&v,&w);
		g[u].push_back({v,w});
		g[v+n].push_back({u+n,w});	
	} 
	dijkstra(1,d1);
	memset(p,0,sizeof(p));
	dijkstra(n+1,d2);
	for(int i=2;i<=n;i++)
		ans+=d1[i]+d2[n+i];
	cout<<ans<<endl;
	return 0;
}
2024/5/28 23:45
加载中...