跳到正文

个人记录

A+B

123lusicheng约 2 分钟
原文
文章信息与版本

文章信息

发布
2024年2月20日
收录
2026年10月26日
更新
2026年11月13日
篇幅
约 2 分钟

版本

  1. v1当前
C++
a+b问题
#include<bits/stdc++.h>
using namespace std;
int head[100000],cnt;
long long ans[1000000];
bool vis[1000000];
int m,n,s,o;
struct edge
{
	int to;
	int nextt;
	int wei;
}edge[1000000];
struct priority{
    int ans;
    int id;
    bool operator <(const priority &x)const
    {
        return x.ans<ans;
    }
};
void addedge(int x,int y,int z)
{
	edge[++cnt].to=y;
	edge[cnt].wei=z;
	edge[cnt].nextt=head[x];
	head[x]=cnt;
}
priority_queue<priority> q;
int main()
{
	cin>>m>>n>>s>>o;
	for(int i=1;i<=n;i++)
		ans[i]=2147483647;
	ans[s]=0;
	for(int i=1;i<=n;i++)
	{
		int a,b,c;
		cin>>a>>b>>c;
		addedge(a,b,c);
	}
	int u;
	q.push((priority){0,s});
    while(!q.empty())
    {
        priority temp=q.top();
        q.pop();
        u=temp.id;
        if(!vis[u])
        {
        	vis[u]=1;
	        for(int i=head[u];i;i=edge[i].nextt)
	        {
	            int v=edge[i].to;
	            if(ans[v]>ans[u]+edge[i].wei)
	            {
	                ans[v]=ans[u]+edge[i].wei;
	                if(!vis[v])
	                    q.push((priority){ans[v],v});
	            }
	        }
		}
    }
	cout<<ans[o];
}
原文