题目链接:点这里!
In this winter holiday, Bob has a plan for skiing at the mountain resort.
This ski resort has M different ski paths and NNdifferent flags situated at those turning points.
The ii-th path from the S_iSi-th flag to the T_iTi-th flag has length Li.
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≤10000and 0 < M \leq 1000000<M≤100000 as 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 5 4 1 3 3 2 3 4 3 4 1 3 5 2
样例输出复制
6
题意:给你n个点,m条滑道,接着m行代表任意两个点和她们之间的距离,求所有路径中最长的那条路径的长度!
思路:STL解决。
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
#include<vector>
using namespace std;
const int maxn = 1e4+10;
int in[maxn],d[maxn];
int n,m,u,v,w;
vector<pair<int,int> >G[maxn]; //注意pair的用法
void topsort()
{
queue<int>q;
for(int i=1;i<=n;i++)
{
if(!in[i])
q.push(i); //将入度为0的点加入队列
}
int now;
while(!q.empty())
{
now=q.front(),q.pop();
for(int i=0;i<G[now].size();i++)
{
if(--in[G[now][i].first]==0) //判断与now相连得下一条边的入度
{
q.push(G[now][i].first);
}
//孩子节点的最大长度为max(孩子节点的最大长度,父亲节点的最大长度+父亲到孩子这条边的权值)
d[G[now][i].first] = max(d[G[now][i].first],d[now]+G[now][i].second);
}
}
}
int main()
{
ios::sync_with_stdio(false);
int t;
cin>>t;
while(t--)
{
cin>>n>>m;
memset(d,0,sizeof(d));
memset(in,0,sizeof(in));
for(int i=1;i<=n;i++)
G[i].clear();
for(int i=0;i<m;i++)
{
cin>>u>>v>>w;
G[u].push_back(make_pair(v,w)); //用make_pair()嵌入数据
in[v]++;
}
topsort();
int max1 = -1;
for(int i=1;i<=n;i++)
max1 = max(max1,d[i]); //d[i]里面的值已经存好了
cout<<max1<<endl;
}
return 0;
}