transaction transaction transaction

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 88    Accepted Submission(s): 39

Problem Description

Kelukin is a businessman. Every day, he travels around cities to do some business. On August 17th, in memory of a great man, citizens will read a book named "the Man Who Changed China". Of course, Kelukin wouldn't miss this chance to make money, but he doesn't have this book. So he has to choose two city to buy and sell.
As we know, the price of this book was different in each city. It is ai yuan in it city. Kelukin will take taxi, whose price is 1yuan per km and this fare cannot be ignored.
There are n−1 roads connecting n cities. Kelukin can choose any city to start his travel. He want to know the maximum money he can get.

 
Input

The first line contains an integer T (1≤T≤10) , the number of test cases.
For each test case:
first line contains an integer n (2≤n≤100000) means the number of cities;
second line contains n numbers, the ith number means the prices in ith city; (1≤Price≤10000)
then follows n−1 lines, each contains three numbers x, y and z which means there exists a road between x and y, the distance is zkm (1≤z≤1000).

(1≤z≤1000).

 
Output
For each test case, output a single number in a line: the maximum money he can get.

 
Sample Input
1
4
10 40 15 30
1 2 30
1 3 2
3 4 10
 
Sample Output
8
 

题目链接:HDU 6201

这题感觉还是蛮有意思的,由于以前被拆点的题目坑过,看到这题就是求未知起点的最长路,但是它有边权,也有点权啊怎么办,可以把点拆成入点和出点,然后构造源点S和终点T,然后这样连边(由于我用最长路求,显然记买入和路费为负,卖出为正):

$<i,i+n,0>$,自身拆点肯定要连;

$<u+n,v,-dis>$、$<v+n,u,-dis>$,由于要求价值最大,花费显然要负权;

$<S,i,-price[i]>$,由于未知起点,那么直接把点都连到S上从S开始,并且这样刚好可以把点权转换成边权

$<i+n,T,price[i]>$,卖掉第i个后到T点。

然后这样写了之后感觉没什么问题就交了,反正是1A了。

代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <bitset>
#include <string>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 100010;
struct edge
{
int to, nxt, d;
edge() {}
edge(int _to, int _nxt, int _d): to(_to), nxt(_nxt), d(_d) {}
} E[N * 5];
int head[N << 1], tot;
bitset < N << 1 > vis;
int d[N << 1];
int price[N]; void init()
{
CLR(head, -1);
tot = 0;
}
inline void add(int s, int t, int d)
{
E[tot] = edge(t, head[s], d);
head[s] = tot++;
}
void spfa(int s)
{
CLR(d, -INF);
vis.reset();
vis[s] = 1;
d[s] = 0;
queue<int>Q;
Q.push(s);
while (!Q.empty())
{
int u = Q.front();
Q.pop();
vis[u] = 0;
for (int i = head[u]; ~i; i = E[i].nxt)
{
int v = E[i].to;
if (d[v] < d[u] + E[i].d)
{
d[v] = d[u] + E[i].d;
if (!vis[v])
{
vis[v] = 1;
Q.push(v);
}
}
}
}
}
int main(void)
{
int T, n, a, b, dx, i;
scanf("%d", &T);
while (T--)
{
init();
scanf("%d", &n);
int S = 0, T = 2 * n + 1;
for (i = 1; i <= n; ++i)
{
scanf("%d", &price[i]);
add(i, i + n, 0);
add(S, i, -price[i]);
add(i + n, T, price[i]);
}
for (i = 1; i < n; ++i)
{
scanf("%d%d%d", &a, &b, &dx);
add(a + n, b, -dx);
add(b + n, a, -dx);
}
spfa(S);
printf("%d\n", d[T]);
}
return 0;
}

HDU 6201 transaction transaction transaction(拆点最长路)的更多相关文章

  1. HDU 4514并查集判环+最长路

    点击打开链接 题意:中文题...... 思路:先推断是否能成环,之前以为是有向图,就用了spfa推断,果断过不了自己出的例子,发现是无向图.并查集把,两个点有公共的父节点,那就是成环了,之后便是求最长 ...

  2. HDU 6201 2017沈阳网络赛 树形DP或者SPFA最长路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6201 题意:给出一棵树,每个点有一个权值,代表商品的售价,树上每一条边上也有一个权值,代表从这条边经过 ...

  3. hdu 1534(差分约束+spfa求最长路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1534 思路:设s[i]表示工作i的开始时间,v[i]表示需要工作的时间,则完成时间为s[i]+v[i] ...

  4. HDU 6201 transaction transaction transaction(树形DP)

    transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/1 ...

  5. HDU - 6201 transaction transaction transaction(树形dp取两点)

    transaction transaction transaction Kelukin is a businessman. Every day, he travels around cities to ...

  6. 2017ICPC沈阳网络赛 HDU 6201 -- transaction transaction transaction(树上dp)

    transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/1 ...

  7. hdu 6201 transaction transaction transaction

    https://vjudge.net/contest/184514#problem/H 题意: 一个商人为了赚钱,在城市之间倒卖商品.有n个城市,每个城市之间有且只有一条无向边连通.给出n个城市的货物 ...

  8. HDU 6201 transaction transaction transaction (树形DP)

    题意:给定一棵树,每个点有一个点权,每条边也是,找一条路径,问你 T-S-sum,T表示路径的终点的权值,S表示路径始点的权值,sum表示从S到T的边权和. 析:把这一条路径拆开来看,那么就是必然是从 ...

  9. HDU - 6201:transaction transaction transaction(最长路)

    Kelukin is a businessman. Every day, he travels around cities to do some business. On August 17th, i ...

随机推荐

  1. JDK9 新特性

    JDK9 新特性目录导航 目录结构 模块化系统 jshell 多版本兼容JAR 接口的私有方法 改进try-with-resourcs 改进砖石操作符 限制使用单独下划线标识符 String存储结构变 ...

  2. JS 控制文本框禁止输入例子

    JS 控制不能输入特殊字符 <input type="text"class="domain"onkeyup="this.value=this.v ...

  3. elasticsearch 5.x 系列之三 mapping 映射的时候的各个字段的设置

    首先看来创建一个mapping 来show show: curl -XPUT "master:9200/zebra_info?pretty" -H 'Content-Type: a ...

  4. 网站安全检测 漏洞检测 对thinkphp通杀漏洞利用与修复建议

    thinkphp在国内来说,很多站长以及平台都在使用这套开源的系统来建站,为什么会这么深受大家的喜欢,第一开源,便捷,高效,生成静态化html,第二框架性的易于开发php架构,很多第三方的插件以及第三 ...

  5. C语言学习记录_2019.01.29

    C语言的灵魂:指针 #include <stdio.h> int main(int argc, char **argv) {  printf("Hello, World!\n&q ...

  6. 复位自动ID的问题有兩種方法

    复位自动ID的问题 有兩種方法:      方法1:      truncate   table   你的表名   --這樣不但將數據刪除,而且可以重新置位identity屬性的字段.         ...

  7. Linux中的目录功能(Red Hat 7)

    目录的基本功能: /bin:存放普通用户使用的命令 /sbin:存放管理员可以执行的命令 /home:存放普通的家目录 如张三家目录为/home/zhangsan /root:管理员的家目录 /etc ...

  8. Fabric go sdk初始化所需证书解析

    fabric sdk go 提供的官方文档少之又少,要想入门,主要就靠研究官方的e2e系列示例,这真的是一件挺无奈的事情.没法子,只能硬着头皮上了.研究发现,e2e这个例子是通过cryptogen生成 ...

  9. R语言学习笔记(三):零碎知识点(1-10)

    1--c() c表示"连接"(concatenate). 在R中向量是连续存储的,因此不能插入或删除元素. 2--seq() seq()的特殊用法,可以用在for循环里for(i ...

  10. Hadoop数据倾斜及解决办法

    数据倾斜:就是大量的相同key被partition分配到一个分区里,map /reduce程序执行时,reduce节点大部分执行完毕,但是有一个或者几个reduce节点运行很慢,导致整个程序的处理时间 ...