HDU 4171 Paper Route

Problem Description
As a poor, tuition-ridden student, you've decided to take up a part time job as a paperboy/papergirl.
You've just been handed your paper route: a set of addresses (conveniently labelled 1 to N).

Every morning, you start at the newspaper office (which happens to be address number 0). You have to plan a route to deliver a newspaper to every address - and you also want to get to class right after you're done.
Conveniently, there are only N roads in your area connecting the addresses, and each of them takes a known time to traverse.
Also, you've precalculated the time it takes to get to Waterloo campus from each address, including the newspaper office (through some combination of biking, busing, or hitching a ride).
How soon can you be done delivering papers and be in your seat at school?
 
Input
Input consists of a number of cases, for each case:
First, there will be a single integer N (the number of addresses, 1 ≤ N ≤ 100,000).
Next, there will be N+1 lines, each with an integer ci (starting with i = 0, 0 ≤ ci ≤ 1,000,000,000), the time it takes to get from location i to campus.
Finally, the input will contain N lines, each with three integers a, b, c (0 ≤ a, b ≤ N, a != b, 0 ≤ c ≤ 1,000). Each of these lines describes a road between locations a and b taking c minutes to traverse.
It is guaranteed that you will be able to reach all the addresses. (Remember that location 0 is the newspaper office.)
 
Output
Output the minimum time it will take to deliver all the papers and get to class.
 
Sample Input
2 1 3 4 0 1 1 0 2 2
Sample Output
7
 
Source

树的性质:从跟节点出发遍历一颗树的所有节点再回到跟节点的花费为一定为他的所有的权值之和的2倍。

 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <vector>
 4 #define MAXN 110000
 5 using namespace std;
 6 
 7 struct Node{
 8     int end;
 9     int w;
10 };
11 
12 int N;
13 int dist[MAXN];
14 bool visited[MAXN];
15 vector<Node> V[MAXN];
16 __int64 c[MAXN];
17 __int64 sum;
18 
19 void addEdge(int u ,int v, int w){
20     Node n1,n2;
21     n1.end=v;
22     n1.w=w;
23     V[u].push_back(n1);
24     n2.end=u;
25     n2.w=w;
26     V[v].push_back(n2);
27 }
28 
29 void dfs( int u ){
30     int size=V[u].size();
31     for(int i=0; i<size; i++){
32         Node now=V[u][i];
33         if( !visited[now.end] ){
34             dist[now.end]=dist[u]+now.w;
35             sum+=now.w;
36             visited[now.end]=1;
37             dfs(now.end);
38         }
39     }
40 }
41 
42 int main()
43 {
44     while( scanf("%d",&N)!=EOF ){
45         for(int i=0; i<=N; i++){
46             scanf("%I64d" ,&c[i]);
47         }
48         for(int i=0; i<=N; i++){
49             V[i].clear();
50         }
51         memset(dist ,0 ,sizeof(dist));
52         memset(visited ,0 ,sizeof(visited));
53         int u,v,w;
54         for(int i=1; i<=N; i++){
55             scanf("%d %d %d" ,&u ,&v ,&w);
56             addEdge(u ,v ,w);
57         }
58         sum=0;
59         visited[0]=1;
60         dfs(0);
61         __int64 ans=2*sum;
62         __int64 min=2*sum+c[0];
63         for(int i=1; i<=N; i++){
64             if( ans-dist[i]+c[i]<min )
65                 min=ans-dist[i]+c[i];
66         }
67         printf("%I64d
" ,min);
68     }    
69     return 0;
70 }
原文地址:https://www.cnblogs.com/chenjianxiang/p/3617943.html