H.Skiing

In this winter holiday, Bob has a plan for skiing at the mountain resort.

This ski resort has MM different ski paths and NN different flags situated at those turning points.

The ii-th path from the S_iS​i​​-th flag to the T_iT​i​​-th flag has length L_iL​i​​.

Each path must follow the principal of reduction of heights and the start point must be higher than the end point strictly.

An available ski trail would start from a flag, passing through several flags along the paths, and end at another flag.

Now, you should help Bob find the longest available ski trail in the ski resort.

Input Format

The first line contains an integer TT, indicating that there are TT cases.

In each test case, the first line contains two integers NN and MM where 0 < N \leq 100000<N≤10000 and 0 < M \leq 1000000<M≤100000 as described above.

Each of the following MM lines contains three integers S_iS​i​​, T_iT​i​​, and L_i~(0 < L_i < 1000)L​i​​ (0<L​i​​<1000) describing a path in the ski resort.

Output Format

For each test case, ouput one integer representing the length of the longest ski trail.

样例输入

1
5 4
1 3 3
2 3 4
3 4 1
3 5 2

样例输出

6

代码:
#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;
typedef pair<int,int> P;
vector<P>g[N];
int a[N],f[N];
queue<int> q;
bool vis[N];
int main()
{
int t;
ci(t);
while(t--)
{
int n,m;
memset(f,, sizeof(f));
memset(a,, sizeof(a));
ci(n),ci(m);
for(int i=;i<=n;i++) g[i].clear();
for(int i=;i<m;i++){
int x,y,z;
ci(x),ci(y),ci(z);
g[x].push_back(P(y,z));
a[y]++;// 入度
}
for(int i=;i<=n;i++){
if(!a[i]) q.push(i); //入度点
}
int ma=;
while(q.size())//按拓扑序来找最长路
{
int u=q.front();
q.pop();
for(int i=;i<g[u].size();i++){
int v=g[u][i].first;
int d=g[u][i].second;
f[v]=max(f[v],f[u]+d);
ma=max(ma,f[v]);
a[v]--;
if(!a[v]) q.push(v);
}
}
pi(ma);
} }
 

2017 ACM-ICPC(乌鲁木齐赛区)网络赛 H.Skiing 拓扑排序+最长路的更多相关文章

  1. Skiing 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛H题(拓扑序求有向图最长路)

    参考博客(感谢博主):http://blog.csdn.net/yo_bc/article/details/77917288 题意: 给定一个有向无环图,求该图的最长路. 思路: 由于是有向无环图,所 ...

  2. 【2017 ACM/ICPC 乌鲁木齐赛区网络赛环境测试赛 E】蒜头君的排序

    [链接]h在这里写链接 [题意] 在这里写题意 [题解] 莫队算法+树状数组. 区间增加1或减少1. 对逆序对的影响是固定的. (用冒泡排序变成升序的交换次数,就是逆序对的个数) [错的次数] 0 [ ...

  3. 2014 ACM/ICPC 鞍山赛区网络赛(清华命题)

    为迎接10月17号清华命题的鞍山现场赛 杭电上的题目 Biconnected(hdu4997)     状态压缩DP Rotate(hdu4998)    相对任一点的旋转 Overt(hdu4999 ...

  4. 2017 ACM/ICPC 南宁区 网络赛 Overlapping Rectangles

    2017-09-24 20:11:21 writer:pprp 找到的大神的代码,直接过了 采用了扫描线+线段树的算法,先码了,作为模板也不错啊 题目链接:https://nanti.jisuanke ...

  5. hdu 4438 第37届ACM/ICPC 天津赛区现场赛H题

    题意:Alice和Bob两个人去打猎,有两种(只)猎物老虎和狼: 杀死老虎得分x,狼得分y: 如果两个人都选择同样的猎物,则Alice得分的概率是p,则Bob得分的概率是(1-p): 但是Alice事 ...

  6. 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 H Skiing【拓扑排序,关键路径】

    2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛  H Skiing In this winter holiday, Bob has a plan for skiing at the moun ...

  7. 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 M. Frequent Subsets Problem【状态压缩】

    2017 ACM-ICPC 亚洲区(南宁赛区)网络赛  M. Frequent Subsets Problem 题意:给定N和α还有M个U={1,2,3,...N}的子集,求子集X个数,X满足:X是U ...

  8. HDU 5875 Function -2016 ICPC 大连赛区网络赛

    题目链接 网络赛的水实在太深,这场居然没出线zzz,差了一点点,看到这道题的的时候就剩半个小时了.上面是官方的题意题解,打完了才知道暴力就可以过,暴力我们当时是想出来了的,如果稍稍再优化一下估计就过了 ...

  9. 2017 ACM-ICPC网络赛 H.Skiing 有向图最长路

    H.Skiing In this winter holiday, Bob has a plan for skiing at the mountain resort. This ski resort h ...

随机推荐

  1. 使用语音识别JAVA SDK 的MAVEN源代码制作语音控制智能家居Java APP-------MAVEN工程加载问题解决

    一直想做一个可以录音的可执行JAVA APP,实现自然语言对话. 第一步就是实现把录音转成语义,比如你对着话筒说"你好",你获取回答相应的回复.你对着话筒说"今天的天气& ...

  2. vc类型转换函数大全

    windows c++中存在各种类型,在实际应用过程中也需要将类型互相转换,故整理了常用类型之间的转换并将之封装成函数,仅供参考,有什么不对的地方,还请指正!   ****************** ...

  3. 笨办法用js屏蔽被http劫持的浮动广告

    最近发现网站经常在右下角弹出一个浮动广告,开始的时候以为只是浏览器的广告. 后来越来越多同事反映在家里不同浏览器也会出现广告.然后深入检查了下,发现网站竟然被劫持了. 然后百度了一大堆资料,什么htt ...

  4. akoj-1280另类阶乘问题

    另类阶乘问题 Time Limit:3000MS  Memory Limit:65536K Total Submit:22 Accepted:20 Description 大家都知道阶乘这个概念,举个 ...

  5. 一步一步学MySQL-一致性非锁定读和锁定读

    一致性非锁定读(consistent nonlocking read) 一致性非锁定读是值InnoDB存储引擎通过多版本控制(multi versioning)的方式来读取当前执行时间数据库中的数据. ...

  6. [Mysql] 安装后启动不了

    Mysql安装后启动报错: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql ...

  7. JavaScript版—贪吃蛇小组件

    最近在学习JavaScript,利用2周的时间看完了<JavaScript高级编程>,了解了Js是一门面向原型编程的语言,没有像C#语言中的class,也没有私有.公有.保护等访问限制的级 ...

  8. Qt图片按原比例缩放

    1.选择图片 QString strFilePath = QFileDialog::getOpenFileName(this, tr("Select file"), QStanda ...

  9. 使用ActionBarActivity或者RxAppCompatActivity或者AppCompatActivity闪退的问题

    新建一个项目,Activity继承RxAppCompatActivity的时候,在页面跳转的时候会出现闪退的问题,一直都没有解决. 后面将两个父类全部改成activity,问题解决.但是有的时候必须使 ...

  10. 升级Cocoapods引起的Mantle库找不到的问题及解决方法

    年前升级了Cocoapods库,从0.39升级到了1.2.0-beta版,然后用模拟器和真机测试都是没有问题的,均可以成功编译.今天测试人员要测试包,准备archive打包时,却提示:ld: libr ...