transaction transaction transaction 最大费用最大流转化到SPFA最长路
//当时比赛的时候没有想到可以用SPFA做,TLE!
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.
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).
4
10 40 15 30
1 2 30
1 3 2
3 4 10
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<functional>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL; //最大费用最大流 简化到SPFA上特例
const int MAXN = + ;
struct edge
{
int to, next, cost;
}E[MAXN << ];
int head[MAXN], tot, n;
int val[MAXN];
bool vis[MAXN];
int lowcost[MAXN];
void init()
{
memset(head, -, sizeof(head));
tot = ;
}
void addedge(int u, int v, int d)
{
E[tot].to = v;
E[tot].cost = d;
E[tot].next = head[u];
head[u] = tot++;
}
int spfa(int st, int ed)
{
memset(vis, false, sizeof(vis));
memset(lowcost, , sizeof(lowcost));
queue<int> q;
q.push(st);
vis[st] = true;
lowcost[st] = ;
while (!q.empty())
{
int f = q.front();
q.pop();
vis[f] = false;
for (int i = head[f]; i != -; i = E[i].next)
{
int v = E[i].to, d = E[i].cost;
if (lowcost[v] < lowcost[f] + d)
{
lowcost[v] = lowcost[f] + d;
if (!vis[v])
{
vis[v] = true;
q.push(v);
}
}
}
}
return lowcost[ed];
}
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
init();
scanf("%d", &n);
for (int i = ; i <= n; i++)
{
scanf("%d", &val[i]);
addedge(, i, val[i]);
addedge(i, n + , -val[i]);
}
int u, v, d;
for (int i = ; i < n - ; i++)
{
scanf("%d%d%d", &u, &v, &d);
addedge(u, v, -d);
addedge(v, u, -d);
}
printf("%d\n", spfa(, n + ));
}
}
transaction transaction transaction 最大费用最大流转化到SPFA最长路的更多相关文章
- 【Luogu】P3381最小费用最大流模板(SPFA找增广路)
		题目链接 哈 学会最小费用最大流啦 思路是这样. 首先我们有一个贪心策略.如果我们每次找到单位费用和最短的一条增广路,那么显然我们可以把这条路添加到已有的流量里去——不管这条路的流量是多大,反正它能 ... 
- hdu 6437 /// 最小费用最大流 负花费 SPFA模板
		题目大意: 给定n,m,K,W 表示n个小时 m场电影(分为类型A.B) K个人 若某个人连续看了两场相同类型的电影则失去W 电影时间不能重叠 接下来给定m场电影的 s t w op 表示电影的 开始 ... 
- P1559 运动员最佳匹配问题[最大费用最大流]
		题目描述 羽毛球队有男女运动员各n人.给定2 个n×n矩阵P和Q.P[i][j]是男运动员i和女运动员j配对组成混合双打的男运动员竞赛优势:Q[i][j]是女运动员i和男运动员j配合的女运动员竞赛优势 ... 
- HDU 6201 transaction transaction transaction(拆点最长路)
		transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 132768/1 ... 
- hdu 6501 transaction transaction transaction 最长路/树形DP/网络流
		最长路: 设置一个虚拟起点和虚拟终点,每个点与起点间一条负边,值为这个点书的价值的相反数(代表买书花钱),每个点与终点连一条正边,值为这个点的书的价格(代表卖书赚钱). 然后按照图中给的边建无向边,权 ... 
- 【BZOJ】1221: [HNOI2001] 软件开发(最小费用最大流)
		http://www.lydsy.com/JudgeOnline/problem.php?id=1221 先吐槽一下,数组依旧开小了RE:在spfa中用了memset和<queue>的版本 ... 
- Doctor NiGONiGO’s multi-core CPU(最小费用最大流模板)
		题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=693 题意:有一个 k 核的处理器和 n 个工作,全部的工作都须要在一个核上处理一个单位的 ... 
- [BZOJ2324][ZJOI2011][最小费用最大流]营救皮卡丘
		[Problem Description] 皮卡丘被火箭队用邪恶的计谋抢走了!这三个坏家伙还给小智留下了赤果果的挑衅!为了皮卡丘,也为了正义,小智和他的朋友们义不容辞的踏上了营救皮卡丘的道路. 火箭队 ... 
- 2018牛客网暑期ACM多校训练营(第五场) E - room - [最小费用最大流模板题]
		题目链接:https://www.nowcoder.com/acm/contest/143/E 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ... 
随机推荐
- Windows API函数大全一
			1. API之网络函数 WNetAddConnection 创建同一个网络资源的永久性连接 WNetAddConnection2 创建同一个网络资源的连 ... 
- Android图片压缩,不失真,上线项目
			当然了,图片压缩是利用了libjpeg库的基础上,牛逼的同学可以自行生成so.jar.在此给出一个链接: http://www.cnblogs.com/hrlnw/p/4403334.html 在生成 ... 
- filter 过滤器加载流程
			过滤器例子 <!--A过滤器--><filter> <filter-name>mdamptRightLimitFilter</filter-name> ... 
- Godaddy域名301跳转问题处理
			前言:Godaddy的域名301跳转一共有六步,详情见以下步骤: 第一步: 第二步:找到你的域名,并点击DNS 第三步:点击添加 第四步:添加解析ip地址 第五步:域名转址,也就是301跳转 第六步: ... 
- php接收json格式数据(text/xml)
			在API服务中,目前流行采用json形式来交互. 给前端调用的接口输出Json数据,这个比较简单,只需要组织好数据,用json_encode($array) 转化一下,前端就得到json格式的数据. ... 
- [Android]如何实现无限滚动的ListViw/GridView(翻译)
			ListView和GridView已经成为原生的Android应用实现中两个最流行的设计模式.目前,这些模式被大量的开发者使用,主要是因为他们是简单而直接的实现,同时他们提供了一个良好,整洁的用户体验 ... 
- vue2.0 vue.extend()的拓展
			<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ... 
- CREATE CONVERSION - 定义一个用户定义的码制转换
			SYNOPSIS CREATE [DEFAULT] CONVERSION name FOR source_encoding TO dest_encoding FROM funcname DESCRIP ... 
- Vim中文编码问题
			1.影响中文编码的设置项 encoding(enc):encoding是Vim的内部使用编码,encoding的设置会影响Vim内部的Buffer.消息文字等.在 Unix环境下,encoding的默 ... 
- HTTP请求报文与响应报文格式
			请求报文包含三部分: a.请求行:包含请求方法.URI.HTTP版本信息 b.请求首部字段 c.请求内容实体 响应报文包含三部分: a.状态行:包含HTTP版本.状态码.状态码的原因短语 b.响应首部 ... 
