CSU1830-FarAway-拓扑排序/最长路

Q: FarAway

Description

Thai University lecturers often have to work all the time even when they are sleeping. The only time that they can rest is when they are going to academic events, such as conferences, seminar, etc. Since the travel time to such an event is considered work time, a group of ICT instructors came up with an idea to use that travel time to simply sleep. Hence, whenever they get to choose which academic events to travel to, they always choose the one that is farthest away from the university. More recently, they’ve become a little picky: they’ll only go if the total travel time is no less than a threshold M.
You will be presented with multiple cities (vertices) and a number of flight options (directed edges). The cities are numbered. The instructors will start from vertex 1. This is where the university is located. Every city that appears in the input is a valid destination (i.e., it has an academic event). However, traveling to an academic event often requires connecting through a number of cities. But each city has exactly one direct connection into it. This means, the input is a directed tree where every vertex, except for vertex 1, has precisely one incoming edge.

Input

The first line contains a number, T, the number of test cases where 0 < T <= 20. For each of the following test cases, the first line contains two numbers, C, and M. Here, C (1 < C <= 100000) is the number of cities, and M (1 <= M <= 100,000,000) is the “pickiness” threshold (i.e., the minimum travel time for a trip to be worthwhile). For the next C-1 lines, each line contains three numbers, C1, C2, and D, representing a directed edge of distance D (0 < D <= 100) from C1 to C2. Here, C1 and C2 are cities, so 1 <= C1, C2 <= C.
All input numbers are integers. When there are multiple integers on the same line, they are each separated by a single white space.

Output

For each test case, a single number in a single line, the distance of the longest route. However, you will output -1 if there is no route with distance at least M from the university to anywhere (see the sample test case #2).

Sample Input

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

Sample Output

2
-1
6

题意就是求源点到各点最长路,然后看最长的那条是否大于等于给定的M值
直接拓扑排序然后就求各点最长路,然后就遍历一遍就好了,主要是要搞一个拓扑排序的模板
AC代码:

#include <cctype>
#include <algorithm>
#include <cstring>
#include <list>
#define LL long long
#define N 100100
#define INF 0x3f3f3f3f
using namespace std;
struct Edge{
    int from,to,cost;
    Edge(int a,int b,int c) : from(a),to(b),cost(c){}
    Edge()=default;
};
Edge edges[400400];
list<int> l;
vector<int> G[N];
bool V[N];
int indeg[N];
LL mp[N];
int cnt=0,n,m;
void bfs()
{
    queue<int> q;
    q.push(1);
    V[1]=true;
    while(!q.empty()){
        int u=q.front();
        q.pop();
        l.push_back(u);
        for(int i=0;i<G[u].size();++i){
            int v=edges[G[u][i]].to;
            indeg[v]--;
            if(indeg[v]==0&&!V[v]){
                V[v]=true;
                q.push(v);
            }
        }
    }
}
int main()
{
    int a,b,c,t;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        memset(mp,0,sizeof(mp));
        memset(V,false,sizeof(V));
        memset(indeg,0,sizeof(indeg));
        for(int i=1;i<=n;++i){
            G[i].clear();
        }
        l.clear();
        for(int i=0;i<n-1;++i){
            scanf("%d%d%d",&edges[i].from,&edges[i].to,&edges[i].cost);
            indeg[edges[i].to]++;
            G[edges[i].from].push_back(i);
        }
        bfs();
            for(int i=0;i<G[*a].size();++i){
                mp[edges[G[*a][i]].to]=max(mp[edges[G[*a][i]].to],mp[edges[G[*a][i]].from]+edges[G[*a][i]].cost);
            }
        }
        int ans=0;
        for(int i=1;i<=n;++i){
            if(ans<mp[i]){
                ans=mp[i];
            }
        } 
        cout<<(ans<m?-1:ans)<<endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值