文章标题 CSU 1830: FarAway
来源:互联网 发布:gis绘图软件 编辑:程序博客网 时间:2024/05/17 23:34
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
Hint
Source
ACM-ICPC Asia Thailand National On-Site Programming Contest 2015
题意:有一颗树,然后顶点1是树根,然后给出一个m,问顶点1到达其他顶点的最大距离是否大于 m 。
分析:由于题意说是一棵树,所以顶点1到达其他点的路径就是确定的,那么距离也就确定的,所以只需要bfs跑一下得到顶点1到达其他点的距离就行了,然后用最大值与m比较一下。
一开始题意没理解好题意用的dij写的也能过,而且跑的时间也少一点,感谢
CSS_ac
的评论指正。
代码:
bfs版
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <queue>#include <set>#include <map>#include <algorithm>#include <math.h>#include <vector>using namespace std;typedef long long ll;const int mod=1e9+7;const int maxn=1e5+10;int dis[maxn];struct Edge{ int v,val;};vector<Edge>G[maxn];void bfs(){ queue<int>q; q.push(1); dis[1]=0; while (!q.empty()){ int u=q.front();q.pop(); for (int i=0;i<G[u].size();i++){ int v=G[u][i].v; dis[v]=dis[u]+G[u][i].val; q.push(v); } }}int C,M;int main(){ int T; scanf ("%d",&T); while (T--){ scanf ("%d%d",&C,&M); for (int i=0;i<=C;i++)G[i].clear(); int u,v,val; for (int i=0;i<C-1;i++){ scanf ("%d%d%d",&u,&v,&val); G[u].push_back(Edge{v,val}); } bfs(); int ans=0; for (int i=1;i<=C;i++){ ans=max(dis[i],ans); } if (ans>=M)printf ("%d\n",ans); else printf ("-1\n"); } return 0;}
dij版
#include<iostream>#include<string>#include<cstdio>#include<cstring>#include<vector>#include<math.h>#include<map>#include<queue> #include<algorithm>using namespace std;const int inf = 0x3f3f3f3f;const int maxn=100005;struct node { int v; int cost; node (int v_=0,int cost_=0){ v=v_;cost=cost_; } bool operator <(const node &t)const{ return cost>t.cost; }};struct Edge{ int v,cost; Edge(int v_=0,int cost_=0){ v=v_;cost=cost_; }};vector <Edge> G[maxn];void add(int u,int v,int val){ G[u].push_back(Edge(v,val));}int n,m;int dis[maxn];//最短距离 int vis[maxn];void dijkstra(){ for (int i=0;i<=n;i++){ dis[i]=10000000+100; vis[i]=0; } dis[1]=0; vis[1]=1; priority_queue<node>q; q.push(1); while (!q.empty()){ node tmp=q.top();q.pop(); int u=tmp.v; for (int i=0;i<G[u].size();i++){ Edge tt=G[u][i]; if (dis[tt.v]>dis[u]+tt.cost){ dis[tt.v]=dis[u]+tt.cost; if (vis[tt.v]==0){ vis[tt.v]=1; q.push(node(tt.v,dis[tt.v])); } } } }}int main (){ int t; scanf ("%d",&t); while (t--){ scanf ("%d%d",&n,&m); for (int i=0;i<=n;i++)G[i].clear(); int u,v,val; for (int i=0;i<n-1;i++){ scanf ("%d%d%d",&u,&v,&val); add(u,v,val); } dijkstra(); int ans=0; int flag=0; for (int i=1;i<=n;i++){ ans=max(dis[i],ans);//找答案 } if (ans<m)printf ("-1\n"); else printf ("%d\n",ans); } return 0;}
- 文章标题 CSU 1830: FarAway
- CSU-1830 FarAway
- 文章标题 CSU 1756 :Prime
- 文章标题 CSU 1836 :Square(高精度)
- 文章标题 CSU 1829: Dungeon(BFS)
- 文章标题 CSU 1832: Jump(DP)
- 文章标题 CSU 1856: Sokoban(模拟)
- 文章标题 CSU 1578 : 爬楼梯 (简单DP)
- 文章标题 CSU 1815 : Enterprising Escape(BFS--优先队列)
- 文章标题 CSU 1831: Found(矩阵快速幂)
- 文章标题 CSU 1838: Water Pump (单调栈)
- 文章标题 CSU 1843: Jumping monkey (状态压缩+dp)
- 文章标题 CSU 1913 (线段树+二分答案)
- 文章标题 CSU 1846: Assembly line(DP)
- 文章标题 CSU 1859: Gone Fishing(贪心)
- 文章标题CSU 1862: The Same Game(模拟)
- 文章标题 CSU 1845: Sensor network (暴力LCA+kruskal思想)
- 文章标题 CSU 1849: Comparing answers(矩阵降维+随机化思想)
- C# 常用设计模式 值得收藏
- consul 手动注册服务
- HDU
- apk通过runtime.getruntime().exec(monkey)执行
- 技术与架构,解析如何将大数据最快落地到实践
- 文章标题 CSU 1830: FarAway
- AsyncTask和Handler的优缺点比较
- 九度OJ-1104-整除问题
- 扉页
- C++游戏实例--五子棋
- 2017湖南多校第二场-20170312
- VMware 创建Linux虚拟机
- 这家数据公司为什么能成为数百万企业的选择?
- CCF201612-3权限查询