竞赛讨论区 > 菜鸡提问
头像
后飞的笨鸟
发布于 2023-04-27 23:27
+ 关注

菜鸡提问

#include<iostream> 
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
typedef long long LL;
const int N=2e6+10;
const LL M=2e10+50;
typedef pair<LL, int> PII;
int h[N],e[N],ne[N],idx;
int n,m;
bool st[N];
LL w[N],dist[N];
LL dijkstra()
{
    for(int i=1;i<=n;i++)	dist[i]=M;
    dist[1] = 0;
    priority_queue<PII, vector<PII>, greater<PII>> heap;
    heap.push({0, 1});      // first存储距离,second存储节点编号

    while (heap.size())
    {
        auto t = heap.top();
        heap.pop();

        int ver = t.second;
		LL	distance = t.first;

        if (st[ver]) continue;
        st[ver]=true;

        for (int i = h[ver]; i != -1; i = ne[i])
        {
            int j = e[i];
            if (dist[j] > distance + w[i]){
                dist[j] = distance + w[i];
                heap.push({dist[j], j});
            }
        }
    }
	
	if(dist[n]==M)		return -1;
	else return dist[n];
}

void add(int a,int b,LL c){
	e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
}


int main(){
	scanf("%d%d",&n,&m);
	memset(h,-1,sizeof h);
	int a,b;
    LL c;
	while(m--){
		scanf("%d%d%lld",&a,&b,&c);
		add(a,b,c),add(b,a,c);
	}
	
	LL ans=dijkstra();
	
	if(ans==-1)	printf("qwb baka\n");
	else printf("%lld\n",ans);
	return 0;
}


第58行,为啥直接写 add ( a , b , c ),过不去。

全部评论

(2) 回帖
加载中...
话题 回帖

本文相关内容

等你来战

查看全部

热门推荐