79分求助。。。不知道为啥超时
查看原帖
79分求助。。。不知道为啥超时
152527
Abby_Mote楼主2021/8/12 16:44
#include <bits/stdc++.h>
using namespace std;
struct node {
	int now,val;
	bool operator<(const node &b) const{
		return this->val > b.val;
	}
};

struct edge{
	int e,v,next;
};

int n,m,edg_cnt=0,head[200005],mark[200005],num[200005],cnt=0,ans=0;
edge edg[100005];

void add_edg(int a,int b,int c){
	edg[edg_cnt].e=b;
	edg[edg_cnt].v=c;
	edg[edg_cnt].next=head[a];
	head[a]=edg_cnt++;
}

int main(){
	scanf("%d%d",&n,&m);
	memset(head,-1,sizeof(head));
	memset(num,0x3f,sizeof(num));
	for(int i=0;i<m;i++){
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		add_edg(a,b,c);
		add_edg(b,a,c);
	}
	priority_queue<node> que;
	que.push((node){ n,0 });
	num[n]=0;
	while(!que.empty()){
		node temp=que.top();
		que.pop();
		if(mark[temp.now])  continue;
		mark[temp.now] = 1;
		ans+=temp.val;
		cnt++;
		if(cnt==n) break;
		for(int i=head[temp.now];i!=-1;i=edg[i].next){
			int e=edg[i].e,v=edg[i].v;
			if(mark[e]==0&&num[e]>v){
				num[e]=v;
				que.push((node){ e,v });
			}
		}
	}
	if(cnt==n) printf("%d\n",ans);
	else printf("orz\n");
	return 0;
} ```
2021/8/12 16:44
加载中...