Ink最近得到了一张藏宝图,这张图上共有n个藏宝室,但因为年代久远藏宝图上的路已经模糊不清,于是Ink找到了智慧的Pseudo,Pseudo告诉Ink,这个宝藏中每两个藏宝室之前都有一条通路,每条通路上都有一定数量的机关。现在Ink已经探明了其中n-1条路上的机关数目,这n-1条路不构成任何环路。众所周知Ink是个乐天派,他坚持认为自己探明的这些路构成的无环联通子图中机关总数是唯一且最少的,现在假设他的想法是对的,他想知道整个图中最少共有多少机关?
标签:
#include<cstdio> #include<vector> #include<cstring> #include<algorithm> using namespace std; const int INF = 0x7FFFFFFF; const int maxn = 105; int jz[maxn][maxn]; vector<int> ljb[maxn]; int anss, flag; int cost[maxn]; int ff[maxn]; void dfs(int start,int end,int now,int tot) { int i; if (now == end) { int maxn = -1; flag = 1; for (i = 0; i < tot - 1; i++) { if (jz[cost[i]][cost[i + 1]]>maxn) maxn = jz[cost[i]][cost[i + 1]]; } anss = anss + maxn + 1; return; } for (i = 0; i < ljb[now].size(); i++) { if (ff[ljb[now][i]] == 0) { ff[ljb[now][i]] = 1; cost[tot] = ljb[now][i]; dfs(start, end, ljb[now][i], tot + 1); if (flag) return; } } } int main() { int n, i, j, u, v, c; while (~scanf("%d", &n)) { anss = 0; for (i = 0; i <= n; i++) for (j = 0; j <= n; j++) jz[i][j] = INF; for (i = 0; i <= n; i++) ljb[i].clear(); for (i = 1; i <= n - 1; i++) { scanf("%d%d%d", &u, &v, &c); jz[u][v] = c; jz[v][u] = c; anss = anss + c; ljb[u].push_back(v); ljb[v].push_back(u); } for (i = 1; i <= n; i++) { for (j = i + 1; j <= n; j++) { if (jz[i][j] == INF) { memset(ff, 0, sizeof(ff)); ff[i] = 1; cost[0] = i; flag = 0; dfs(i, j, i, 1); } } } printf("%d\n", anss); } return 0; }
标签:
原文地址:http://www.cnblogs.com/zufezzt/p/4523683.html