FZU1096,QS Network

题解:

把网络适配器的价格加的网络电缆的价格中,再对新的价格图跑一遍prim就行了

#include
#include
#include
#include
#include
using namespace std;
int t,n;
int maz[1009][1009];
int price[1009];
bool vis[1009];
int dis[1009];
void prim()
{
    int i,j,t,pos,sum;
    memset(vis,false,sizeof(vis));
    memset(dis,0,sizeof(dis));
    for (i = 1; i <= n; ++i)
    {
        dis[i] = maz[1][i];
    }
    vis[1] = true;
    sum = 0;
    for (i = 0; i < n-1; ++i)
    {
        t = INT_MAX;
        for (j = 1; j <= n; ++j)
        {
            if (!vis[j] && t > dis[j])
            {
                t = dis[j];
                pos = j;
            }
        }
        sum += t;
        vis[pos] = true;
        for (j = 1; j <= n; ++j)
        {
            if (!vis[j] && dis[j] > maz[pos][j])
            {
                dis[j] = maz[pos][j];
            }
        }
    }
    printf("%d\n",sum);
}
int main()
{
    int i,j;
    while (~scanf("%d",&t))
    {
        while (t--)
        {
            scanf("%d",&n);
            for (i = 1; i <= n; ++i)
            {
                scanf("%d",&price[i]);
            }
            for (i = 1; i <= n; ++i)
            {
                for (j = 1; j <= n; ++j)
                {
                    scanf("%d",&maz[i][j]);
                }
            }
            for (i = 1; i <= n; ++i)//把网络适配器的价格加到网络电缆的价格中去
            {
                for (j = i+1; j <= n; ++j)
                {
                    maz[i][j] += price[i];
                    maz[i][j] += price[j];
                    maz[j][i] = maz[i][j];
                }
            }
            prim();
        }
    }
    return 0;
}


你可能感兴趣的:(图论,最小生成树,FZU1096,QS,Network)