题意:有n个点,n-1条边的无向图,已知每个点书的售价,以及在边上行走的路费,问任选两个点作为起点和终点,能获得的最大利益是多少。

分析:

1、从某个结点出发,首先需要在该结点a花费price[a]买书,然后再在边上行走,到达目的地后,在目的地b获得price[b]。

2、因此可以建立两个虚拟结点,

虚拟结点1连向n个点,边权分别为-price[i],表示以i为起点,需花费price[i]买书。

n个点连向虚拟结点2,边权分别为price[i],表示以i为终点,通过卖书可得price[i]。

3、spfa求最长路即可得最大利益。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const LL MOD = 998244353;
const double pi = acos(-1.0);
const int MAXN = 100000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int n;
struct Edge{
int v, cost;
Edge(int vv = 0, int c = 0):v(vv), cost(c){}
};
vector<Edge> E[MAXN];
void addEdge(int u, int v, int w){
E[u].push_back(Edge(v, w));
}
bool vis[MAXN];
int cnt[MAXN], dist[MAXN];
bool SPFA(int start){
memset(vis, false, sizeof vis);
memset(cnt, 0, sizeof cnt);
for(int i = 0; i < MAXN; ++i) dist[i] = -INT_INF;
queue<int> q;
while(!q.empty()) q.pop();
q.push(start);
dist[start] = 0;
vis[start] = true;
cnt[start] = 1;
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(int i = 0; i < E[u].size(); ++i){
int v = E[u][i].v;
if(dist[v] < dist[u] + E[u][i].cost){
dist[v] = dist[u] + E[u][i].cost;
if(!vis[v]){
vis[v] = true;
q.push(v);
if(++cnt[v] > n) return false;
}
}
}
}
return true;
}
int price[MAXN];
int main(){
int T;
scanf("%d", &T);
while(T--){
for(int i = 0; i < MAXN; ++i) E[i].clear();
scanf("%d", &n);
for(int i = 1; i <= n; ++i){
scanf("%d", &price[i]);
}
int x, y, z;
for(int i = 0; i < n - 1; ++i){
scanf("%d%d%d", &x, &y, &z);
addEdge(x, y, -z);
addEdge(y, x, -z);
}
for(int i = 1; i <= n; ++i){
addEdge(0, i, -price[i]);
addEdge(i, n + 1, price[i]);
}
SPFA(0);
printf("%d\n", dist[n + 1]);
}
return 0;
}

  

HDU - 6201 transaction transaction transaction(spfa求最长路)的更多相关文章

  1. XYZZY(spfa求最长路)

    http://acm.hdu.edu.cn/showproblem.php?pid=1317 XYZZY Time Limit: 2000/1000 MS (Java/Others)    Memor ...

  2. spfa求最长路

    http://poj.org/problem?id=1932 spfa求最长路,判断dist[n] > 0,需要注意的是有正环存在,如果有环存在,那么就要判断这个环上的某一点是否能够到达n点,如 ...

  3. POJ 3592--Instantaneous Transference【SCC缩点新建图 &amp;&amp; SPFA求最长路 &amp;&amp; 经典】

    Instantaneous Transference Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6177   Accep ...

  4. 洛谷 P3627 [APIO2009]抢掠计划 Tarjan缩点+Spfa求最长路

    题目地址:https://www.luogu.com.cn/problem/P3627 第一次寒假训练的结测题,思路本身不难,但对于我这个码力蒟蒻来说实现难度不小-考试时肛了将近两个半小时才刚肛出来. ...

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

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

  6. POJ 3126 --Father Christmas flymouse【scc缩点构图 &amp;&amp; SPFA求最长路】

    Father Christmas flymouse Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 3007   Accep ...

  7. HDU 1224 Free DIY Tour(spfa求最长路+路径输出)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1224 Free DIY Tour Time Limit: 2000/1000 MS (Java/Oth ...

  8. [HDU 1317]XYZZY[SPFA变形][最长路]

    题意: 一个图, 点权代表走到该点可获得的能量值. 可正可负. 一个人从1 号出发,带有100点能量. 问是否有一种方案可使人在能量值>0的时候走到n. 思路: 这个题首先要注意点权. 其实就是 ...

  9. 【HDOJ1217】【Floyd求最长路】

    http://acm.hdu.edu.cn/showproblem.php?pid=1217 Arbitrage Time Limit: 2000/1000 MS (Java/Others)    M ...

随机推荐

  1. day21-Python运维开发基础(单个字符匹配 / 多字符匹配)

    1. 正则表达式(单个字符匹配) # ### 正则表达式 => 单个字符匹配 import re """ lst = re.findall(正则表达式,字符串) & ...

  2. Spring 各个组件架构

  3. pdf.js-----后端返回utf-8数据流,前端处理数据展示pdf

    需求:做项目联调接口时,发现知识库展示pdf未果,经与后端人员沟通,发现以下问题: 1.接口返回的是utf-8数据流,但是前端调用的是base64解析方法: 导致功能有误: 方案一:将后端返回的utf ...

  4. gets和scanf区别

    scanf 和 gets 读取字符串 深入了解scanf()/getchar()和gets()等函数 scanf与gets函数读取字符串的区别 今天看到一段话,大致是说gets比scanf()快,有点 ...

  5. Numpy 广播(Broadcast)

    广播(Broadcast)是 numpy 对不同形状(shape)的数组进行数值计算的方式,对数组的算术运算通常在相应的元素上进行. 如果两个数组 a 和 b 形状相同,即满足a.shape == b ...

  6. C. Gas Pipeline DP

    C. Gas Pipeline time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  7. Python学习第三课——运算符

    # 运算符 + - * / **(幂) %(取余) //(取整) num=9%2 print("余数为"+(str)(num)) #运算结果为 1 num1=9//2 print( ...

  8. java的浅拷贝和深拷贝(待解决)

    1.什么是浅拷贝,什么是深拷贝? 2.storm的并行度问题,需要使用全局变量static ConcorrentHashMap,因为加了static,所有的线程只能拷贝该全局变量的一个唯一的副本,进行 ...

  9. Cent OS下配置虚拟Ip地址

    1.首先我们登录操作系统 用户名root 密码 123456 然后我们输入ip查询命名 ip addr  也可以输入 ifconfig查看ip,但此命令会出现3个条目,centos的ip地址是ens3 ...

  10. Android中ListView结合CheckBox判断选中项

    本文主要实现在自定义的ListView布局中加入CheckBox控件,通过判断用户是否选中CheckBox来对ListView的选中项进行相应的操作.通过一个Demo来展示该功能,选中ListView ...