Problem Description
也许,人活着就是要尝试人世间的酸甜苦辣,喜怒哀乐,经历从婴儿到老人的一个过程吧!能看,能想,能爱,能恨,这就是活人与死人的区别。不要想着死后会怎样,谁也不知道。所以要好好的活着,宽待自己,好好珍惜身边的人!
现在,上帝之手宽恕与你。把你人生中所要经历的所有的酸甜苦辣全部摆在你的面前。你一生需要经历n种事情。每种事情都有一个权值x。上帝给了你n-1个时光隧道。你可以把时光隧道放在任意两种事情的中间,如果你把时光隧道放在a,b中间,那么你需要花费3*(xa+xb)+7 。现在,你需要把这n-1个时光隧道连在n种事情中间,使得任意两种事情都会相连,并且花费最小。输出最小花费。
Input
首先输入一个数T,代表有T组数据。
接下来一行输入一个数n。
接下来的一行输入n个数,第i个数代表第i种事情的权值。
n<=1000,每种事情的权值<=100000;
T<=60
Output
输出T行,每行输出最小花费。
Example Input
1 2 1 2
Example Output
16
Hint
Author
zp
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e3 + 5; const int INF = 0x3f3f3f3f; bool vis[maxn]; int dis[maxn], Map[maxn][maxn], a[maxn]; void CSH() { memset(Map, INF, sizeof(Map)); for(int i = 0; i <= maxn; i++) { Map[i][i] = 0; } memset(vis, 0, sizeof(vis)); } void SR(int m) { for(int i = 1; i <= m; i++) { scanf("%d", &a[i]); } for(int i = 1; i < m; i++) { for(int j = i + 1; j <= m; j++) { int c = 3 * (a[i] + a[j]) + 7; if(c < Map[i][j]) { Map[i][j] = Map[j][i] = c; } } } } void Prim(int n) { for(int i = 1; i <= n; i++) { dis[i] = Map[1][i]; } dis[1] = 0; vis[1] = 1; int sum = 0; for(int i = 1; i < n; i++) { int ans = INF; int flag; for(int j = 1; j <= n; j++) { if(!vis[j] && dis[j] < ans) { ans = dis[j]; flag = j; } } vis[flag] = 1; sum += ans; for(int j = 1; j <= n; j++) { if(!vis[j] && dis[j] > Map[flag][j]) { dis[j] = Map[flag][j]; } } } printf("%d\n", sum); } int main() { int T; scanf("%d", &T); while(T--) { int n, m; scanf("%d", &n); CSH(); SR(n); Prim(n); } return 0; } /*************************************************** User name: Result: Accepted Take time: 692ms Take Memory: 2120KB Submit time: 2018-02-01 15:24:51 ****************************************************/