檢查站點(FZU_2195) 前趨圖

 Problem 2195 檢查站點

Accept: 200    Submit: 476
Time Limit: 1000 mSec    Memory Limit : 32768 KB

 Problem Description

在山上一共有N個站點需要檢查,檢查員從山頂出發去各個站點進行檢查,各個站點間有且僅有一條通路,檢查員下山前往站點時比較輕鬆,而上山時卻需要額外的時間,問最後檢查員檢查完所有站點時所需要的額外時間最少是多少。

 Input

包含多組數據 每組數據輸入第一行爲一個整數N 表示站點個數(1<=N<=100000),接下去N-1 行 每行3個整數 x,y,z(1<=z<=10000) 檢查站x爲檢查站y的父節點,x,y之間有一條通路,從y到x需要額外z的時間。(父節點在子節點上方,山頂固定標號爲1)

 Output

輸出一行一個整數表示最少需要花費的額外時間。

 Sample Input

61 2 12 4 11 3 13 5 13 6 1

 Sample Output

3

 Source

福州大學第十二屆程序設計競賽


題目大意:。。。。

解題思路:先假設最後要回到山頂,那麼總值爲所有權之和;但是因爲最後不必回到山頂,那麼要得到最小消耗,就是選擇一條最大的從根節點到葉子節點的作爲,最後下山的的路徑;求該路徑的上的權之和可以用前趨圖做;


代碼如下:

#include"iostream"
#include"cstring"
#include"cstdio"
using namespace std;

struct p{
	int pre;
	bool isrear;
	int va;
};

p tree[100000 + 5];
int x, y, val, sum; 

int max(int a, int b){
	return a > b ? a : b;
}

int main(){
	int n, p, t;
	while(scanf("%d",&n) != EOF){
		sum = 0;
		for(int i = 1;i <= n-1;i ++){
			scanf("%d%d%d", &x, &y, &val);
			tree[y].pre = x;
			tree[y].va = val;
			tree[y].isrear = true;
			tree[x].isrear = false;
			sum += val;
		}
		int m = 0;
		for(int i = 1;i <= n;i ++){
			if(tree[i].isrear == true){
				t = 0;
				p = i;
				while(p != 1){
					t += tree[p].va;
					p = tree[p].pre;
				}
				m = max(t, m);
			}
		}
		printf("%d\n",sum - m);
	}
}


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