John loves winter. Every skiing season he goes heli-skiing with his friends. To do so, they rent a helicopter that flies them directly to any mountain in the Alps. From there they follow the picturesque slopes through the untouched snow.

 Of course they want to ski on only the best snow, in the best weather they can get. For this they use a combined condition measure and for any given day, they rate all the available slopes.

 Can you help them find the most awesome route?

 Input Format
The input consists of: one line with two integers nn ( \le n \le )(≤n≤) and mm ( \le m \le )(≤m≤),where nn is the number of (-indexed) connecting points between slopes and mm is the number of slopes.
mm lines, each with three integers ss,tt,cc ( \le s,t \le n, \le c \le )(≤s,t≤n,≤c≤) representing a slopefrom point ss to point tt with condition measure cc.
Points without incoming slopes are mountain tops with beautiful scenery, points without outgoing slopes are valleys. The helicopter can land on every connecting point, so the friends can start and end their tour at any point they want. All slopes go downhill, so regardless of where they start, they cannot reach the same point again after taking any of the slopes. Output Format
Output a single number nn that is the maximum sum of condition measures along a path that the friends could take Hint 样例输入1 复制 样例输出1 复制 样例输入2 复制 样例输出2 复制 题目来源
German Collegiate Programming Contest ​

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cmath>
typedef long long ll;
#define lowbit(x) (x&(-x))
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
using namespace std;
#define pi acos(-1)
#define P pair<ll,ll>
const int N =5e3+;//开始把B设为了1000,一直 段错误,m <=5000
const ll inf =1e12+;
int n,m;
ll u,v,w,cnt;
ll d[N],head[N];
struct Edge{
ll fr,to,val,nex;
Edge(){}
Edge(ll fr,ll to,ll val,ll nex):fr(fr),to(to),val(val),nex(nex){}
}e[N*];
void add(ll u,ll v,ll w){
e[cnt].fr=u;
e[cnt].to=v;
e[cnt].val=w;
e[cnt].nex=head[u];
head[u]=cnt++;
}
void init()
{
for(int i =;i<N;i++){
d[i] = ;//求最大值
head[i] = -;
}
cnt = ;
}
void bfs()
{
priority_queue<P,vector<P>,greater<P> >Q;
for(int i =;i<=n;i++)
Q.push(P(,i));//终点为I的路线的距离为0(i到i)
while(!Q.empty())
{
P tmp = Q.top();
Q.pop();
ll v= tmp.second;
if(d[v] > tmp.first) continue;//距离短的路线不用走了。
for(ll i=head[v];i!=-;i=e[i].nex){
Edge ee =e[i];
ll t= e[i].to;
if(d[t]<d[v]+ee.val){
d[t]=d[v]+ee.val;
Q.push(P(d[t],t));
}
} }
}
int main()
{
scanf("%d%d",&n,&m);
init();
for(int i=;i<m;i++){
scanf("%lld%lld%lld",&u,&v,&w);
add(u,v,w);//有向图
}
bfs();
ll maxx = -inf;
for(int i =;i<=n;i++) maxx=max(maxx,d[i]);//找到一条路线距离最长
printf("%lld\n",maxx);
return ;
}

最新文章

  1. 游戏机制(Machinations)在线演示工具
  2. LeetCode 328. Odd Even Linked List
  3. DSO动态加载PHP模块到Apache服务器
  4. 【转】PowerShell入门(三):如何快速地掌握PowerShell?
  5. Linux设备驱动工程师之路——内核链表的使用【转】
  6. ScrollView嵌套StackView提示需要宽度和高度限制
  7. bootstrap知识小点
  8. web.xml中的url-pattern映射规则
  9. PowerDesigner实用技巧小结(4)
  10. Android 2D绘图初步
  11. Ubuntu下Qt项目的部署
  12. springMVC的拦截器工作流程
  13. 在Windows上使用Ubuntu共享的打印机
  14. Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)(A.思维题,B.思维题)
  15. You earned your Program Management Professional (PgMP)&#174; Credential
  16. 会跳高的字体插件jquery.beattext.js
  17. Javascript继承4:洁净的继承者----原型式继承
  18. 深入理解Java中的synchronized锁重入
  19. 一对一 只需将另一个表的id设置为主键和外键即可
  20. javaweb项目中errorPage的问题

热门文章

  1. js重载的实现
  2. HTTPS与SSL(一)
  3. C++ 宏定义的简单使用
  4. MeshLab中插件的添加过程
  5. SqlServer报错:主体“dbo”不存在
  6. HTML和CSS一般有哪些功能?(聊~平时常出现的那些知识)
  7. android RadioGroup设置某一个被选中
  8. 2018.2.7 css 的一些方法盒子模型
  9. Windows环境下在Oracle VM VirtualBOX下克隆虚拟机镜像(克隆和导入)
  10. spring-boot自定义启动端口