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 题意: 奶牛们没钱了,正在找工作.农夫约翰知道后,希望奶牛们四处转转,碰碰运气. 而 ...
随机推荐
- Linux远程连接和FTP
远程连接: ssh 用户名@IP地址 输入密码 FTP连接: sftp 用户名@IP 输入密码 远程连接支持所有命令,就相当于操作电脑 FTP连接只支持部分命令: ls.cd.get.put get: ...
- 禁止form重复提交
$("form").submit(function () { console.log("提交了"); $("input:submit").a ...
- IO字节流。
字节输出流: java.io.OutputStream :抽象类,是表示输出字节流的所有类的父类. java.io.FileOutputStream extends OutputStream: 文件字 ...
- View模块
一.应用场景 通过View的类注释,可知,Backbone.view是一个JS构造函数,与DOM中的某一块UI相对应,通过注册模型层数据的监听,可实现视图的自动渲染. Backbone.View模块也 ...
- 第2章 TCP-IP的工作方式
第2章 TCP-IP的工作方式 TCP/IP协议系统 为了实现TCP的功能,TCP/IP的创建者使用了模块化的设计.TCP/IP协议系统被分为不同的组件,每个组件分别负责通信过程的一个步骤.这种模块化 ...
- css 选择器 & UI元素的伪类选择器 & 伪元素选择器
UI元素的伪类选择器 1. :focus 用来选取获取焦点事件 2. :enabled 用来指定当元素处于可用状态时的样式 3. :disable 用来指定元素处于不可用时的状态 表单里应用 ...
- Counting blessings can actually increase happiness and health by reminding us of the good things in life.
Counting blessings can actually increase happiness and health by reminding us of the good things in ...
- 配置海康IPC或大华IPC通过路由器公网访问
设备:路由器DLink-DIR-600M,海康IPC:DS-2CD864FWD-E 海康默认端口为8000,HTTP访问为80,RTSP访问端口为554. 配置分成两步,分别为配置IPC相关网络参数和 ...
- mysql 5.7以上安装遇到的问题
参考地址: https://blog.csdn.net/u012278016/article/details/80455439 本人在window上安装mysql 5.7版本以上的mysql,出现很 ...
- Mybatis介绍(一)
这里介绍的mybatis比较简单, 我做为一个初学者, 记录下个人在学习中方法, 如果那里出错, 希望读者朋友们见谅. 首先这里介绍一下我们下面用的表结构: author表是保存了作者的个人信息, 因 ...