nyoj-38-佈線問題

佈線問題

時間限制:1000 ms  |  內存限制:65535 KB
難度:4
描述
南陽理工學院要進行用電線路改造,現在校長要求設計師設計出一種佈線方式,該佈線方式需要滿足以下條件:
1、把所有的樓都供上電。
2、所用電線花費最少
輸入
第一行是一個整數n表示有n組測試數據。(n<5)
每組測試數據的第一行是兩個整數v,e.
v表示學校裏樓的總個數(v<=500)
隨後的e行裏,每行有三個整數a,b,c表示a與b之間如果建鋪設線路花費爲c(c<=100)。(哪兩棟樓間如果沒有指明花費,則表示這兩棟樓直接連通需要費用太大或者不可能連通)
隨後的1行裏,有v個整數,其中第i個數表示從第i號樓接線到外界供電設施所需要的費用。( 0<e<v*(v-1)/2 )
(樓的編號從1開始),由於安全問題,只能選擇一個樓連接到外界供電設備。
數據保證至少存在一種方案滿足要求。
輸出
每組測試數據輸出一個正整數,表示鋪設滿足校長要求的線路的最小花費。
樣例輸入
1
4 6
1 2 10
2 3 10
3 1 10
1 4 1
2 4 1
3 4 1
1 3 5 6
樣例輸出
4


/*
    problem : NYOJ38 佈線問題
    stratege: kruscal algorithm
    URL :http://acm.nyist.net/JudgeOnline/problem.php?pid=38
    status :166386	1006100213	佈線問題	Accepted	 76	 4216	C/C++	05-13 21:57:42

*/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std ;

const int MAXN = 505 ;
const int INF = 0xfffffff ;
int mat[MAXN][MAXN] ;
int vset[MAXN] ;
int add[MAXN] ;
int v, e, ans ;

struct Node
{
    int a, b ;
    int w ;
};
Node MST[MAXN*MAXN] ;

bool cmp (const Node &t1, const Node &t2)
{
    return t1.w < t2.w ;
}

void init ()
{
    int i, k = 1 ;
    int c, d, t ;
    scanf ("%d%d", &v, &e) ;

    for (i = 1; i <= e; i ++)
    {
        scanf ("%d%d%d", &c, &d, &t) ;
        MST[k].a = c ;
        MST[k].b = d ;
        MST[k].w = t ;
        k++ ;
    }
    for (i = 1; i <= v; i ++)
    {
        vset[i] = i ;
        scanf ("%d", &add[i]) ;
    }

    sort (add+1, add+v+1) ;
    sort (MST+1, MST+1+e, cmp) ;
}

void kruscal ()
{
    int i, j, tag = 1 ;
    int num = 1 ;
    int u1, v1, sn1, sn2 ;
    ans = 0 ;

    while (num < v)
    {
        u1 = MST[tag].a ;
        v1 = MST[tag].b ;
        sn1 = vset[u1] ;
        sn2 = vset[v1] ;

        if (sn1 != sn2)
        {
            num ++ ;
            ans += MST[tag].w ;
            for (i = 1; i <= v; i ++)
                if (vset[i] == sn2)
                    vset[i] = sn1 ;

        }
        tag ++ ;
    }

}

int main ()
{
    int tcase ;
    scanf ("%d", &tcase) ;
    while (tcase --)
    {
        init () ;
        kruscal () ;
        printf ("%d\n", ans + add[1]) ;
    }
    return 0 ;
}




 
#include<queue>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<numeric>
#include<algorithm>
using namespace std;
#define CLR(arr,val) memset(arr,val,sizeof(arr))
struct Node
{
	Node(){}
	Node(int num,int len):len(len),num(num){}
	int len,num;
};
bool operator<(const Node& n1,const Node& n2)
{
	return n1.len>n2.len;
}
const int MAX=510;
const int MAXE=250000;
int Head[MAX],Next[MAXE],Num[MAXE],Len[MAXE];
int Dis[MAX],top;
void add(int u,int v,int len)
{
	Num[top]=v;
	Next[top]=Head[u];
	Len[top]=len;
	Head[u]=top++;
}
bool InQ[MAX];
int main()
{
	//freopen("input.txt","r",stdin);
	priority_queue<Node> q;
	int t,m,n,a,b,l;
	scanf("%d",&t);
	while(t--)
	{
		top=0;CLR(Head,-1);
		CLR(Dis,0x3f);
		scanf("%d%d",&m,&n);
		for(int i=0;i!=n;i++)
		{
			scanf("%d%d%d",&a,&b,&l);
			add(a-1,b-1,l);add(b-1,a-1,l);
		}
		Dis[0]=0;
		q.push(Node(0,0));
		while(!q.empty())
		{
			Node t=q.top();q.pop();
			if(Dis[t.num]!=t.len) continue;
			for(int i=Head[t.num];i!=-1;i=Next[i])
			{
				if(Dis[Num[i]]>Len[i])
				{
					Dis[Num[i]]=Len[i];
					q.push(Node(Num[i],Len[i]));
				}
			}
		}
		int minl=0x3f3f3f3f;
		for(int i=0;i!=m;i++)
		{
			scanf("%d",&l);
			minl=min(minl,l);
		}
		printf("%d\n",accumulate(Dis,Dis+m,0)+minl);
	}
}        















發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章