prim63求助(3TLE+1WA)
查看原帖
prim63求助(3TLE+1WA)
220824
yyz1005楼主2021/12/16 13:21
#include<iostream>
#include<cstdio>
#include<queue>
#include<string>
#include<cstring>
using namespace std; 
struct node{
	int id;
	int ml;
	const bool operator < (node b) const{
        return ml>b.ml;
    }
};
int a[5100][5100],n,m;
priority_queue<node> v;
priority_queue<node> u;
node make(int x,int y){
	node ret;
	ret.id = x;
	ret.ml = y;
	return ret;
}
void prim(){
	int cnt = 1,cost=0;
	node vlist[5100];
	int len=1;
	u.push(make(1,0));
	for(int i = 2; i <= n; i++) v.push(make(i,a[1][i]));
	while(cnt<=n&&!v.empty()){
		 node vfi = v.top();
		 v.pop();
		 u.push(vfi);
		 //printf("the best one:%d-%d\n",vfi.id,vfi.ml);
		 cost+=vfi.ml;
		 cnt++;
		 len = v.size();
		 for(int i = 1; i <= len; i++){
		 	vlist[i] = v.top();
		 	v.pop();
		 	vlist[i].ml = min(vlist[i].ml,a[vlist[i].id][vfi.id]);
		 }
		 for(int i = 1; i <= len; i++){
		 	v.push(vlist[i]);
		 }
	}
	if(cnt!=n) cout << "orz";
	else cout << cost;
}
int main(){
	cin >> n >> m;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++) a[i][j] = 100000;
	}
	for(int i = 1,x,y,z; i <= m; i++){
		cin >> x >> y >> z;
		//cout << a[x][y] << " " << z <<"-----";
		a[x][y] = min(a[x][y],z);
		a[y][x] = a[x][y];
		//cout << a[x][y] << " " << a[y][x] << endl;
	}
	prim();
	return 0;
}

TLE:#8,#9,#10

WA:#13(无解)

2021/12/16 13:21
加载中...