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. MVC中的Controllers和View分别放到单独的项目中

    将Controllers放到独立项目中: 第一步:创建Mvc.Controllers,Mvc.Models和UI三个项目 Mvc.Controllers用来编写Controllers Mvc.Mode ...

  2. netty使用以及聊天小程序

    <从零开始搭建游戏服务器>Netty导入创建Socket服务器 Netty入门教程 Netty 聊天小程序

  3. jQuery之方法绑定(事件注册)代码小结

    1.最直接的模式,直接将一个function对象传入方法函数,如下面的click(),好处坏处一看便知 $("#btnComfirmChooseCompany").click(fu ...

  4. EFCore扩展Select方法(根据实体定制查询语句)

    EFCore扩展Select方法(根据实体定制查询语句)  通常用操作数据库的时候查询返回的字段是跟 我们的定义的实体是不一致的,所以往往针对UI或者接口层创建大量的Model, 而且需要手动对应字段 ...

  5. c++实现多叉树树形显示(适合家谱的显示)

    多叉树(左兄弟右孩子二叉树)的树形显示 核心代码 void positionadd(Multiway_tree*root, int n) { if (!root)return; Multiway_tr ...

  6. 80端口被system(pid=4)占用

    1.查看80端口被哪个进程占用,cmd->netstat -ano | findstr 80 2.cmd->tasklist列出当前运行中的进程,或在任务管理器中查看pid为4的进程. 经 ...

  7. Jmeter环境搭建详细介绍

    [前言] 欢迎来到我的博客,知识在于分享,如有不足之处,希望指出,大家共同进步学习! [JDK检查和安装] 现在市面上比较普遍的性能测试工具无非就LoadRunner和Jmeter,本人一直秉持着便宜 ...

  8. 安装 CentOs 系统 及 Python 及 Scrapy 框架

    1: 先安装Centos 系统: 为什么选择CentOs系统,而不选择Ubuntu ? 我在Ubuntu上尝试了三次安装 python 和 Scrapy ,结果都没成功,在运维老王的建议下 使用Cen ...

  9. 关于“java.lang.OutOfMemoryError : unable to create new native Thread”的报错问题

    好吧 我发誓这是postgresql的Mirroring Controller的RT测试的最后一个坑了. 在这个RT测试的最后,要求测试Mirroring Controller功能在长时间运行下的稳定 ...

  10. [jvm]基于jvm的线程实现

    一.线程的实现 学过操作系统的肯定都知道: 进程:是并发执行的程序在执行过程中分配和管理资源的基本单位,是一个动态概念,竞争计算机系统资源的基本单位. 线程:是进程的一个执行单元,是进程内可调度实体. ...