题意:要建立一个网络,使得这个网络上的每个点之间都有通路,建造一条通路的费用是通路两端的点的费用和通路本身的和,已知每个点所需要的花费以及通路的花费,求建造这样一个网络的最小花费是多少。
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1586
思路:最小生成树模板,建图时注意将边权改为通路本身+两个端点的费用和
注意点:无
以下为AC代码:
Run ID | Submit Time | Judge Status | Problem ID | Language | Run Time(ms) | Run Memory(KB) | User Name |
3956923 | 2015-05-09 09:23:58 | Accepted | 1586 | C++0x | 30 | 4232 | luminus |
#include <algorithm>
#include <iostream>
#include <climits>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <deque>
#include <list>
#include <map>
#include <set>
//#include <unordered_map>
//#include <unordered_set>
#define pb push_back
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end()
#define clr(a, v) memset( a , v , sizeof(a) )
#define RS(s) scanf ( "%s", s )
#define RDI(a) scanf ( "%d", &a )
#define RDII(a, b) scanf ( "%d%d", &a, &b )
#define RDIII(a, b, c) scanf ( "%d%d%d", &a, &b, &c )
#define PL() printf ( "\n" )
#define PI(a) printf ( "%d", a )
#define PIL(a) printf ( "%d\n", a )
#define PSL(s) printf ( "%s\n", s )
#define PII(a,b) printf ( "%d %d", a, b )
#define PIIL(a,b) printf ( "%d %d\n", a, b )
#define PIII(a,b,c) printf ( "%d %d %d", a, b, c )
#define PIIIL(a,b,c) printf ( "%d %d %d\n", a, b, c )
#define rep(i,m,n) for ( int i = m; i < n; i ++ )
#define REP(i,m,n) for ( int i = m; i <= n; i ++ )
#define dep(i,m,n) for ( int i = m; i > n; i -- )
#define DEP(i,m,n) for ( int i = m; i >= n; i -- )
#define repi(i,m,n,k) for ( int i = m; i < n; i += k )
#define REPI(i,m,n,k) for ( int i = m; i <= n; i += k )
#define depi(i,m,n,k) for ( int i = m; i > n; i += k )
#define DEPI(i,m,n,k) for ( int i = m; i >= n; i -= k )
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
//#pragma comment ( linker, "/STACK:1024000000,1024000000" )
using namespace std;
const double pi = acos(-1);
template <class T>
inline bool RD ( T &ret )
{
char c;
int sgn;
if ( c = getchar(), c ==EOF )return 0; //EOF
while ( c != '-' && ( c < '0' || c > '9' ) ) c = getchar();
sgn = ( c == '-' ) ? -1 : 1;
ret = ( c == '-' ) ? 0 : ( c - '0' );
while ( c = getchar() , c >= '0' && c <= '9' ) ret = ret * 10 + ( c - '0' );
ret *= sgn;
return 1;
}
inline void PD ( int x )
{
if ( x > 9 ) PD ( x / 10 );
putchar ( x % 10 + '0' );
}
const double eps = 1e-10;
const int dir[4][2] = { 1,0, -1,0, 0,1, 0,-1 };
struct node{
int x, y, cnt;
node(){}
node( int _x, int _y ) : x(_x), y(_y) {}
node( int _x, int _y, int _cnt ) : x(_x), y(_y), cnt(_cnt) {}
};
struct edge{
int to, wei;
edge(){}
edge( int _to, int _wei ) : to(_to), wei(_wei) {}
};
int n;
int pre[1005];
int dis[1005];
int num[1005];
bool vis[1005];
int g[1005][1005];
void init()
{
clr ( g, 0 );
clr ( pre, 0 );
clr ( dis, 0 );
clr ( vis, 0 );
clr ( num, 0 );
}
int prim()
{
int ans = 0;
REP ( i, 1, n ){
dis[i] = g[1][i] + num[1] + num[i];
}
dis[1] = 0;
rep ( k, 0, n ){
int _min = 0x3f3f3f3f;
int p = -1;
REP ( i, 1, n ){
if ( ! vis[i] && _min > dis[i] ){
_min = dis[i];
p = i;
}
}
if ( p == -1 )return -1;
ans += _min;
vis[p] = 1;
REP ( i, 1, n ){
if ( ! vis[i] && dis[i] > g[p][i] + num[i] + num[p] ){
dis[i] = g[p][i] + num[i] + num[p];
}
}
}
return ans;
}
int main()
{
int T;
RDI ( T );
while ( T -- ){
init();
RDI ( n );
REP ( i, 1, n ){
RDI ( num[i] );
}
REP ( i, 1, n ){
REP ( j, 1, n ){
RDI ( g[i][j] );
}
}
int mst = prim();
PIL ( mst );
}
return 0;
}