文章

5

粉丝

0

获赞

0

访问

353

头像
继续畅通工程 题解:prim 仅在读入数据时处理
P1311 浙江大学机试题
发布于2026年2月2日 12:28
阅读数 99

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N =101;
const int inf=0x3f3f3f3f;
int g[N][N];
int cost[N];
bool st[N];
int n;
int prim(){ 
    memset(cost,inf,sizeof cost);
    memset(st,0,sizeof st);
    int cb=0;
    for(int i=0;i<n;i++){ 
        int t=-1;
        for(int j=1;j<=n;j++){
            if(!st[j]&&(t==-1||cost[t]>cost[j]))t=j;
        }
        if(i&&cost[t]==inf)return inf;
        if(i)cb+=cost[t];
        st[t]=1; 
        for(int j=1;j<=n;j++){

           cost[j]=min(cost[j],g[t][j]);
        }
    }
    return cb;
}

int main(){
    while(cin>&...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发