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 ...
随机推荐
- C#控件置于底层或顶层
btn.BringToFront();//置于顶层 btn.SendToBack();//置于底层
- Spring框架学习-搭建第一个Spring项目
步骤一:下载Spring开发包. 官网:https://spring.io/ 下载地址:https://repo.spring.io/libs-release-local/org/ ...
- iOS 中集成百度echarts3.0
突然项目中要用到图表,所以就用了百度的echarts,然后就是网上搜了一下,由于本人的JS不是很熟悉,但是研究了一下还是做出来了,其实也不是很难 最后做的效果大概如下图这种,由于界面上没调整,所以粗糙 ...
- Swiper插件轮播
<html><head> <meta charset="utf-8"> <title>Swiper轮播</title>& ...
- Android 实现对多个EditText的监听
create_account=(EditText)findViewById(R.id.create_account); create_password=(EditText)findViewById(R ...
- vue-cropper
项目中用到了vue-cropper插件,让我觉得很好用附上两个地址 vue-cropper在git上的地址 https://github.com/xyxiao001/vue-cropper 针对vue ...
- CAD使用DeleteXData删除数据(com接口)
主要用到函数说明: MxDrawEntity::DeleteXData 删除扩展数据,详细说明如下: 参数 说明 pzsAppName 删除的扩展数据名称,如果为空,删除所有扩展数据 c#代码实现如下 ...
- R-FCN:Object Detection via Region-based Fully Convolutional Networks
fast.faster这些网络都可以被roi-pooling层分成两个子网络:1.a shared,'fully convolutional' subnetwork 2.an roi-wise sub ...
- MySQL主从复制遇到的问题
show slave status\G时,io显示:Slave_IO_State: Connecting 1.检查网络是否畅通,方法:ping主机ip.主机通畅. 2.检查复制用户的账号密码是否正确. ...
- (thinkPHP)PHP常用函数大全
usleep() 函数延迟代码执行若干微秒.unpack() 函数从二进制字符串对数据进行解包.uniqid() 函数基于以微秒计的当前时间,生成一个唯一的 ID.time_sleep_until() ...