标签:turn class ret size 最小生成树 ace clear names 题意
按题意枚举每个点,建立缺少该点情况下的最小生成树,取权值最大的~
#include<bits/stdc++.h> using namespace std; const int maxn=1014; const int inf=1e9; int g[maxn][maxn]; int visit[maxn]; int d[maxn]; int N,M,x,y; int flag; int prim (int s) { fill (d,d+maxn,inf); d[s]=0; int ans=0; for (int i=1;i<=N-1;i++) { int u=-1,min=inf; for (int j=1;j<=N;j++) if (!visit[j]&&d[j]<min) { u=j; min=d[j]; } if (u==-1) return inf; visit[u]=1; ans+=d[u]; for (int v=1;v<=N;v++) if (!visit[v]&&g[u][v]!=inf&&g[u][v]<d[v]) d[v]=g[u][v]; } return ans; } int main () { scanf ("%d %d",&N,&M); for (int i=1;i<=N;i++) for (int j=1;j<=N;j++) g[i][j]=inf; for (int i=0;i<M;i++) { scanf ("%d %d",&x,&y); scanf ("%d %d",&g[x][y],&flag); if (flag==1) g[x][y]=0; g[y][x]=g[x][y]; } vector<int> vi; int Max=-1; for (int i=1;i<=N;i++) { fill (visit,visit+maxn,0); visit[i]=1; int j=i<N?i+1:1; int mst=prim(j); if (mst>Max) { vi.clear(); vi.push_back(i); Max=mst; } else if (mst==Max) { vi.push_back(i); } } if (Max==0) { printf ("0"); return 0; } for (int i=0;i<vi.size();i++) { if (i!=0) printf (" "); printf ("%d",vi[i]); } return 0; }
PAT T1001 Battle Over Cities-Hard Version
标签:turn class ret size 最小生成树 ace clear names 题意
原文地址:https://www.cnblogs.com/zhanglichen/p/12302739.html