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倍。

 #include <stdio.h>
#include <iostream>
#include <vector>
#define MAXN 110000
using namespace std; struct Node{
int end;
int w;
}; int N;
int dist[MAXN];
bool visited[MAXN];
vector<Node> V[MAXN];
__int64 c[MAXN];
__int64 sum; void addEdge(int u ,int v, int w){
Node n1,n2;
n1.end=v;
n1.w=w;
V[u].push_back(n1);
n2.end=u;
n2.w=w;
V[v].push_back(n2);
} void dfs( int u ){
int size=V[u].size();
for(int i=; i<size; i++){
Node now=V[u][i];
if( !visited[now.end] ){
dist[now.end]=dist[u]+now.w;
sum+=now.w;
visited[now.end]=;
dfs(now.end);
}
}
} int main()
{
while( scanf("%d",&N)!=EOF ){
for(int i=; i<=N; i++){
scanf("%I64d" ,&c[i]);
}
for(int i=; i<=N; i++){
V[i].clear();
}
memset(dist , ,sizeof(dist));
memset(visited , ,sizeof(visited));
int u,v,w;
for(int i=; i<=N; i++){
scanf("%d %d %d" ,&u ,&v ,&w);
addEdge(u ,v ,w);
}
sum=;
visited[]=;
dfs();
__int64 ans=*sum;
__int64 min=*sum+c[];
for(int i=; i<=N; i++){
if( ans-dist[i]+c[i]<min )
min=ans-dist[i]+c[i];
}
printf("%I64d\n" ,min);
}
return ;
}

HDU 4171 Paper Route的更多相关文章

  1. hdu4171 Paper Route 树的性质+DFS

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4171 题意: 有n+1个点,这n+1个点由n条边相连,且保证连通.然后给出各个点到出口的距离,要求从 ...

  2. hdu 4171 最短路

    #include<stdio.h> #include<string.h> #include<queue> #include<iostream> usin ...

  3. hdu 2680 Choose the best route

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Description One day , Kiki ...

  4. hdu 5224 Tom and paper

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5224 Tom and paper Description There is a piece of pa ...

  5. hdu 4240 Route Redundancy 最大流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4240 A city is made up exclusively of one-way steets. ...

  6. HDU 5224 Tom and paper(最小周长)

    HDU 5224 Tom and paper(最小周长) Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d &a ...

  7. [hdu P1599] find the mincost route

    [hdu P1599] find the mincost route 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V ...

  8. HDU 1599 find the mincost route(floyd求最小环 无向图)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1599 find the mincost route Time Limit: 1000/2000 MS ...

  9. HDU 2164 Rock, Paper, or Scissors?

    http://acm.hdu.edu.cn/showproblem.php?pid=2164 Problem Description Rock, Paper, Scissors is a two pl ...

随机推荐

  1. stuff for xml path

    SumOrg=stuff((select '/'+User_Org from V_RubricInfoRefer t where t.RubricID=V_RubricInfoRefer.Rubric ...

  2. EventTrigger

    EventTrigger事件触发器. 相比较数据,属性,事件触发器是XAML的UI层中最重要的一个部分. 事件触发器中,触发的效果是动画,不再是setter. 也是很有意思的 <对象.Style ...

  3. 如何设置linux支持上传的文件中文不乱吗

    一.背景: 1.由于客户的需求,需要a链接打开的pdf文件,支持中文名称的 二.步骤 ①.查看当前编码 locale ②.编辑 vi  /etc/profile 打开后结尾处添加  export LA ...

  4. django-redis 使用规范

    django-redis 基于 BSD 许可, 是一个使 Django 支持 Redis cache/session 后端的全功能组件. 1,安装 django-redis 最简单的方法就是用 pip ...

  5. node.js安装以及git 的使用说明

     第一步:安装node.js: Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境.   Node.js 使用了一个事件驱动.非阻塞式 I/O 的模型,使其轻量又高 ...

  6. docker加速器配置

    我使用docker的原因 最近自己一直在强迫自己使用docker,一方面是docker的容器化服务,使得每一个配置相互独立,易于维护.而且如果到后面如果深入了的话,通过自己编写dockerfile,那 ...

  7. 网址访问量统计插件 FlagCounter

    网址或博客访问量统计插件  ---> FlagCounter. 网址:http://s01.flagcounter.com/more/ERP2/

  8. Qt 学习之路 2(32):贪吃蛇游戏(2)

    Qt 学习之路 2(32):贪吃蛇游戏(2) 豆子 2012年12月27日 Qt 学习之路 2 55条评论 下面我们继续上一章的内容.在上一章中,我们已经完成了地图的设计,当然是相当简单的.在我们的游 ...

  9. Spring学习笔记(一)—— Spring介绍及入门案例

    一.Spring概述 1.1 Spring是什么 Spring是一个开源框架,是于2003年兴起的一个轻量级的Java开发框架, 由Rod Johnson 在其著作<Expert one on ...

  10. flask总结01

    一:Flask的基本介绍和小案例 01:flask的基本介绍 Flask诞生于2010年,是Armin ronacher(人名)用 Python 语言基于 Werkzeug 工具箱编写的轻量级Web开 ...