拓扑排序

悠悠 2022-06-08 06:19 154阅读 0赞



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_iSi​​-th flag to the T_iTi​​-th flag has length L_iLi​​.

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≤100000as described above.

Each of the following MM lines contains three integers S_iSi​​, T_iTi​​, and L_i~(0 < L_i < 1000)Li​​ (0<Li​​<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. 1
  2. 5 4
  3. 1 3 3
  4. 2 3 4
  5. 3 4 1
  6. 3 5 2

样例输出

  1. 6

题目来源

2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛

最长路径:

#include
#include
#include
#include
#include
#include
#define inf 0x3f3f3f
#define ms(x) memset(x,0,sizeof(x))
#define mf(x) memset(x,-1,sizeof(x))
using namespace std;
const int N = 20002;
struct node
{
int y,z;
node() {}
node(int ty, int tz): y(ty), z(tz) {}
};
vectorq[N];
int in[N];
int f[N];
int main()
{
int T;
int n,m;
scanf(“%d”,&T);
while(T—)
{
queueque;
ms(in);
ms(f);
int ans = 0;
scanf(“%d%d”,&n,&m);
for(int i=1; i<=n; i++)
q[i].clear();
for(int i=0; i<m; i++)
{
int x, y, z;
scanf(“%d%d%d”,&x,&y,&z);
q[x].push_back(node(y,z));
in[y]++;
}
for(int i=1; i<=n; i++)
{
if(!in[i])
que.push(i);
}
while(!que.empty())
{
int cur = que.front();
que.pop();
for(int i=0; i<q[cur].size(); i++)
{
int v = q[cur][i].y;
int w = q[cur][i].z;
f[v] = max(f[v],f[cur]+w);
ans = max(ans, f[v]);
in[v]—;
if(!in[v])
que.push(v);
}
}
cout<<ans<<endl;
}
return 0;
}

发表评论

表情:
评论列表 (有 0 条评论,154人围观)

还没有评论,来说两句吧...

相关阅读

    相关 拓扑排序

    拓扑排序是一张AOV网(Activity Of Vertex NetWork),并且是无环的网! 概念: 设G=(V,E)是一个具有n个顶点的有向图,V中的顶点序列V1,V

    相关 拓扑排序

    什么是拓扑排序? 在图论中,拓扑排序(Topological Sorting)是一个有向无环图(DAG, Directed Acyclic Graph)的所有顶点的线性序

    相关 拓扑排序

    拓 扑 排 序 一个较大的工程往往被划分成许多子工程,我们把这些子工程称作活动(activity)。在整个工程中,有些子工程(活动)必须在其它有关子工程完成之后才能开始,也就

    相关 拓扑排序

    (1)有向无环图 无环的有向图,简称 DAG (Directed Acycline Graph) 图。 有向无环图在工程计划和管理方面的应用:除最简单的情况之外,几

    相关 拓扑排序

    拓扑排序: 拓扑排序是根据离散数学中有关偏序与全序定义的。 ![0_1314168765l7fq.gif][] 若一个集合 X 上的关系 R 是自反的 反

    相关 拓扑排序

     一个较大的工程往往被划分成许多子工程,我们把这些子工程称作活动(activity)。在整个工程中,有些子工程(活动)必须在其它有关子工程完成之后才能开始,也就是说,一个子工程