PAT T1001 Battle Over Cities-Hard Version

Posted zhanglichen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT T1001 Battle Over Cities-Hard Version相关的知识,希望对你有一定的参考价值。

按题意枚举每个点,建立缺少该点情况下的最小生成树,取权值最大的~

#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的主要内容,如果未能解决你的问题,请参考以下文章

PAT 1013 Battle Over Cities

PAT1013: Battle Over Cities

PAT A1013 Battle Over Cities (25 分)

PAT Advanced 1013 Battle Over Cities (25分)

pat 1013 Battle Over Cities(25 分) (并查集)

PAT 1013Battle Over Cities