2017 ACM/ICPC Shenyang Online SPFA+无向图最长路
transaction transaction transaction
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 1496 Accepted Submission(s): 723
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<bits/stdc++.h>
//#include<regex>
#define db double
#define ll long long
#define vec vector<ll>
#define Mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define MP make_pair
#define PB push_back
#define fr(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int N=1e6+;
const int mod=1e9+;
const int MOD=mod-;
const db eps=1e-;
const db pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int a[N],d[N],vis[N]; struct P
{
int u,v,w;
P(int x,int y,int z):u(x),v(y),w(z){};
P(){};
};
vector<P> g[N],e;
queue<int> q;
void add(int x,int y,int z)
{
g[x].push_back(P(x,y,z));
}
void spfa(int n)
{
memset(d,, sizeof(d));
memset(vis,, sizeof(vis));
vis[]=;
q.push();
while(q.size())
{
int u=q.front();q.pop();
vis[u]=;
for(int i=;i<g[u].size();i++){
int v=g[u][i].v;
int w=g[u][i].w;
if(d[v]<d[u]+w){// get the maxmum
d[v]=d[u]+w;
if(!vis[v]){
vis[v]=;//push the new point
q.push(v);
}
}
}
}
pi(d[n+]);
}
int main()
{
int t;
ci(t);
while(t--)
{
int n;
ci(n);
for(int i=;i<=n;i++) g[i].clear();
for(int i=;i<=n;i++)
ci(a[i]),add(,i,a[i]),add(i,n+,-a[i]);
for(int i=;i<n;i++){
int x,y,z;
ci(x),ci(y),ci(z);
add(x,y,-z);
add(y,x,-z);
}
spfa(n); } }
2017 ACM/ICPC Shenyang Online SPFA+无向图最长路的更多相关文章
- 2017 ACM/ICPC Asia Regional Shenyang Online spfa+最长路
transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 132768/1 ...
- 2017 ACM ICPC Asia Regional - Daejeon
2017 ACM ICPC Asia Regional - Daejeon Problem A Broadcast Stations 题目描述:给出一棵树,每一个点有一个辐射距离\(p_i\)(待确定 ...
- 2017 ACM - ICPC Asia Ho Chi Minh City Regional Contest
2017 ACM - ICPC Asia Ho Chi Minh City Regional Contest A - Arranging Wine 题目描述:有\(R\)个红箱和\(W\)个白箱,将这 ...
- 2017 ACM/ICPC Asia Regional Qingdao Online
Apple Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submi ...
- XYZZY(spfa求最长路)
http://acm.hdu.edu.cn/showproblem.php?pid=1317 XYZZY Time Limit: 2000/1000 MS (Java/Others) Memor ...
- POJ 3126 --Father Christmas flymouse【scc缩点构图 && SPFA求最长路】
Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 3007 Accep ...
- HDU - 6201 transaction transaction transaction(spfa求最长路)
题意:有n个点,n-1条边的无向图,已知每个点书的售价,以及在边上行走的路费,问任选两个点作为起点和终点,能获得的最大利益是多少. 分析: 1.从某个结点出发,首先需要在该结点a花费price[a]买 ...
- spfa求最长路
http://poj.org/problem?id=1932 spfa求最长路,判断dist[n] > 0,需要注意的是有正环存在,如果有环存在,那么就要判断这个环上的某一点是否能够到达n点,如 ...
- BZOJ 2019 [Usaco2009 Nov]找工作:spfa【最长路】【判正环】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2019 题意: 奶牛们没钱了,正在找工作.农夫约翰知道后,希望奶牛们四处转转,碰碰运气. 而 ...
随机推荐
- spring MVC之注解开发控制器(二)
开发表单控制器 在传统的Spring MVC开发方法中,是通过扩展SimpleFormController类来创建简单的表单控制器.这样就定义了基本的表单处理流程,并允许通过覆盖几个生命周期方法来定制 ...
- orcale开篇
1.数据库系统和数据库的管理系统 数据库系统=数据库的管理系统+oper操作员+硬件2.Oracle的版本 8i/ 9i 10g/11g 12c(cloud)3.实例和数据库的关系 实例:数据 ...
- springboot 学习笔记(六)
(六)springboot整合activemq 1.现下载activemq,下载链接:http://activemq.apache.org/download.html,windows系统解压后进入bi ...
- JAVA反射练习
JAVA反射练习 题目 实现一个方法 public static Object execute(String className, String methodName, Object args[]) ...
- hibernate课程 初探单表映射3-2 基本类型
本节内容:(介绍基本类型) 1 数据类型 简介 2 时间类型 简介 3 时间类型 demo 1 hibernate类型 java类型 integer/int java.lang.Integer/i ...
- HTTPS与SSL(一)
1. HTTPS HTTPS(全称:Hypertext Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版 ...
- Thymeleaf的模板使用介绍
参考网址: https://blog.csdn.net/hry2015/article/details/73476973 先定义一个html文件, 如下: 文件路径: templates/templa ...
- 一位学长的学习建议(java方向)
1.前台总得有个拿的出手的页面能力吧,ajax,jquery不说精通但是至少能看懂,能根据业务需求来改吧. 2.数据库方面至少得玩过mysql,DB2,Oracle中的两个以上吧.hibernate或 ...
- SQL Server 删除当前数据库中所有数据库 ,无视约束
Sql Server中清空所有数据表中的记录 清空所有数据表中的记录: exec sp_msforeachtable @Command1 ='truncate table ?' 删除所有数据表: e ...
- Kibana功能一览
Overview标签 总共32个请求,最大响应时间:4.7秒 Usage标签 可以看到HTTP请求的发起时间分布 Performance and Quality 6个请求里,响应时间在100毫秒以下的 ...