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 c i (starting with i = 0, 0 ≤ c i ≤ 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\n" ,min);
68     }    
69     return 0;
70 }

 

转载于:https://www.cnblogs.com/chenjianxiang/p/3617943.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值