2017 ACM/ICPC Asia Regional Shenyang Online spfa+最长路

时间:2022-04-24 23:35:24

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

Problem Description
Kelukin is a businessman. Every day, he travels around cities to do some business. On August 17th, in memory of a great man, citizens will read a book named "the Man Who Changed China". Of course, Kelukin wouldn't miss this chance to make money, but he doesn't have this book. So he has to choose two city to buy and sell. 
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.
 
Input
The first line contains an integer T (1≤T≤10) , the number of test cases. 
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). 
 
Output
For each test case, output a single number in a line: the maximum money he can get.
 
Sample Input
1
4
10 40 15 30
1 2 30
1 3 2
3 4 10
 
Sample Output
8
 
Source
 
思路:建立一个原点0和汇点n+1,将所有点从0到n+1的可能路径长度取最大值输出即可。
代码:
 #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 Asia Regional Shenyang Online spfa+最长路的更多相关文章

  1. hdu6201&Tab; transaction transaction transaction&lpar;from 2017 ACM&sol;ICPC Asia Regional Shenyang Online&rpar;

    最开始一直想着最短路,不过看完题解后,才知道可以做成最长路.唉,还是太菜了. 先上图: 只要自己添加两个点,然后如此图般求最长路即可,emmm,用SPFA可以,迪杰斯特拉也可以,或者别的都ok,只要通 ...

  2. 2017 ACM&sol;ICPC Asia Regional Shenyang Online(部分题解)

    HDU 6197 array array array 题意 输入n和k,表示输入n个整数和可以擦除的次数k,如果至多擦除k次能是的数组中的序列是不上升或者是不下降序列,就是魔力数组,否则不是. 解题思 ...

  3. HDU 6205&lpar;尺取法&rpar;2017 ACM&sol;ICPC Asia Regional Shenyang Online

    题目链接 emmmm...思路是群里群巨聊天讲这题是用尺取法.....emmm然后就没难度了,不过时间上3000多,有点.....盗了个低配本的读入挂发现就降到2800左右, 翻了下,发现神犇Clar ...

  4. HDU 6198&lpar;2017 ACM&sol;ICPC Asia Regional Shenyang Online&rpar;

    思路:找规律发现这个数是斐波那契第2*k+3项-1,数据较大矩阵快速幂搞定.   快速幂入门第一题QAQ #include <stdio.h> #include <stdlib.h& ...

  5. 2017 ACM&sol;ICPC Asia Regional Shenyang Online array array array

    2017-09-15 21:05:41 writer:pprp 给出一个序列问能否去掉k的数之后使得整个序列不是递增也不是递减的 先求出LIS,然后倒序求出最长递减子序列长度,然后判断去k的数后长度是 ...

  6. 2017 ACM&sol;ICPC Asia Regional Shenyang Online card card card

    题意:看后面也应该知道是什么意思了 解法: 我们设置l,r,符合条件就是l=起始点,r=当前点,不符合l=i+1 学习了一下FASTIO #include <iostream> #incl ...

  7. 2017 ACM&sol;ICPC Asia Regional Shenyang Online transaction transaction transaction

    Problem Description Kelukin is a businessman. Every day, he travels around cities to do some busines ...

  8. 2017 ACM&sol;ICPC Asia Regional Shenyang Online number number number

    题意:求n个斐波那契数列组合都无法得到的最小数字 解法: 1 我们先暴力的求出前面几个数字 2 然后再暴力的求递推 3 接着矩阵快速幂(没写错吧?) /*#include<bits/stdc++ ...

  9. 2017 ACM&sol;ICPC Asia Regional Shenyang Online cable cable cable

    Problem Description Connecting the display screen and signal sources which produce different color s ...

随机推荐

  1. 安装opensuse的笔记-重庆linux开源组织

    mate: 伙伴 matey: 融洽的, 易于亲近的. get matey with sb. poison: a. 有毒的, n.毒药/毒酒v. 下毒, 破坏, 污染 slander [ 撕烂的~~] ...

  2. Hadoop中几个基本命令行命令

    对HDFS操作的几个命令(必须是Hadoop命令,否则只是对本地操作) 1. 查看HDFS中某个目录或文件的包含哪些文件和文件夹: bin/hadoop dfs/fs -ls 文件(夹)路径 若是非空 ...

  3. &lpar;转&rpar;Sqlite 管理工具 SQLiteDeveloper及破解

    本文转载自:http://www.cnblogs.com/error404/archive/2012/03/21/2409898.html 功能特点 表结构设计,数据维护,ddl生成,加密数据库支持, ...

  4. git extrad&lowbar;addons 部署说明

    注册一个git账号 : 网址:  https://github.com/ 1:安装git   sudo apt-get install git 2:  b把urc扩展占模块pull下来    cd   ...

  5. 云脉提供表单识别API接口自助接入

    如今随着信息化.数字化时代的到来,利用纯人工进行数据录入已经不能满足海量信息数字化的需求.这时候有OCR表单识别技术,一切问题都能够迎刃而解了.云脉表单识别SDK采用成熟的OCR技术,通过创建票据的模 ...

  6. 1&period;VMwareTools安装

     1 选中虚拟机.右击.然后点击:安装Vmware-tool(最好是有网络的情况下安装) 2 将Vmware-tool的安装文件复制到暂时文件夹下,截图例如以下: 3 解压VMwareTools- ...

  7. Maven中Spring-Data-Redis存储对象&lpar;redisTemplate&rpar; (转)

    Redis是一种nosql数据库,在开发中常用做缓存.Jedis是Redis在java中的redis- client.在此之前,希望已经了解redis的基本使用和Maven的使用.建立Maven Pr ...

  8. pattern

    A compiled representation of a regular expression. A regular expression, specified as a string, must ...

  9. matplotlib figure图像-【老鱼学matplotlib】

    如果我们想要显示多个图像,有点类似多窗口显示图像这个概念,则就会用到plt.figure() 直接上例子: import numpy as np import pandas as pd import ...

  10. &lbrack;java&comma;2017-12-01&rsqb; 播放音频文件

    废话不多说,直接上代码 jar包 <!-- 2017-12-01音频播放jar包 --> <dependency> <groupId>javazoom</gr ...