Far Far Away

来源:互联网 发布:python 2能装xgboost吗 编辑:程序博客网 时间:2024/04/28 13:03

Page 1 of 2
ACM-ICPC Asia Thailand National On-Site Programming Contest 2015
Problem C
Far Far Away
ACM-ICPC Asia Thailand National On-Site Programming Contest 2015
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).
(See sample test cases in the next page)
Page 2 of 2
ACM-ICPC Asia Thailand National On-Site Programming Contest 2015
Sample Input Sample Output
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
2
-1
6

题目大意:求树根到叶子的最长距离,如果距离大于给定值,则输出最大值,否则输出-1;
解题思路:dfs遍历求根到各个节点的距离,记录其中的最大值即可
总结:多组数据每次都要初始化!!!

#include<iostream>#include<cstdio>#include<vector>#include<queue>#include<cmath>#include<fstream>#include<string.h>using namespace std;int target;int maxdis;struct Edge{    int v,cost;    Edge(int _v,int _cost):v(_v),cost(_cost){}};vector<Edge> G[100005];void addedge(int u,int v,int w){    G[u].push_back(Edge(v,w));}int vis[100005];int dis[100005];void dfs(int u){    vis[u]=1;    int d=G[u].size();    for(int i=0;i<d;i++)    {        int tv=G[u][i].v;        if(!vis[tv])        {            dis[tv]=dis[u]+G[u][i].cost;            maxdis=max(maxdis,dis[tv]);            dfs(tv);        }    }}int main(){    //freopen("in.txt","r",stdin);    int T;    cin>>T;    int u,v,w;    while(T--)    {        memset(vis,0,sizeof(vis));        memset(dis,0,sizeof(dis));        maxdis=0;        int n,m;        cin>>n>>m;        for(int i=0;i<=n;i++)            G[i].clear();        for(int i=1;i<=n-1;i++)        {            cin>>u>>v>>w;            addedge(u,v,w);        }        dfs(1);        if(maxdis>=m)        cout<<maxdis<<endl;        else            cout<<"-1"<<endl;    }    return 0;}
0 0
原创粉丝点击