首页 > 其他分享 >ICPC2017网络赛(乌鲁木齐)H: Skiing (SPFA最长路)

ICPC2017网络赛(乌鲁木齐)H: Skiing (SPFA最长路)

时间:2023-06-02 18:34:38浏览次数:44  
标签:ICPC2017 head ag int resort SPFA Skiing include ski



H: Skiing

 time limit 1000ms memory limit 131072KB
 
i i
In this winter holiday, Bob has a plan for skiing at the mountain resort. This ski resort has M different ski paths and N different flags situated at those turning points. The i-th path from the S -th flag to the T -th flag has length L . 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 T, indicating that there are T cases. In each test case, the first line contains two integers N and M where 0 < N ≤ 10000 and 0 < M ≤ 100000 as described above. Each of the following M lines contains three integers S , T , and L  (0 < L < 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. Sample Input
1 5 4 1 3 3 2 3 4 3 4 1 3 5 2
Sample Output
6
 

【题意】:

给出一个有向无环图,找一条最长路,输出长度

【解析】:

统计入度为0的点,然后对这些点为起点,跑SPFA,同时记忆化搜索记录每个点的最优值。

【代码】:


#include <stdio.h>
#include <stdlib.h>  
#include <string.h>  
#include <iostream>  
#include <algorithm> 
#include <queue>  
#define mset(a,i) memset(a,i,sizeof(a))
using namespace std;
typedef long long ll;
struct node{
    int to,val,next; 
}e[101010];
int n,m,x,y,val,cnt ; 
int IN[10101],head[10101] ;
ll dis[10101];
queue <int> Q ;
void add(int x,int y,int v)
{
    e[cnt] = (node){ y,v,head[x] } ; 
    head[x] = cnt++; 
}
void SPFA(int s)
{
    int u,v;
    while(!Q.empty())Q.pop();
    Q.push(s);
    while(!Q.empty())  
     {
        u=Q.front() ; 
        Q.pop() ;
        for(int i=head[u];~i;i=e[i].next)
        {
            v=e[i].to;
            if(dis[v]<dis[u]+e[i].val)
            {
                dis[v]=dis[u]+e[i].val;
                Q.push(v);
           }
        }
   }
}
int main() 
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		mset(IN,0);
		mset(head,-1);
		mset(dis,0);
		cnt=0;
		scanf("%d%d",&n,&m) ; 
	    for(int i=1;i<=m;i++)
	    {
	    	scanf("%d%d%d",&x,&y,&val);
			add(x,y,val);
	    	IN[y]++;
		}
	     if(n==1)
	     {
	        printf("0\n");
	        continue;
	    }
	    for(int i=1;i<=n;i++)
	    {
	    	if(IN[i]==0)
	  			SPFA(i);
		}
		ll ans=0;
		for(int i=1;i<=n;i++)
		{
			ans=max(ans,dis[i]);
		}
		printf("%lld\n",ans);
	}
    return 0 ;  
}




标签:ICPC2017,head,ag,int,resort,SPFA,Skiing,include,ski
From: https://blog.51cto.com/u_16125110/6404541

相关文章

  • ICPC2017网络赛(南宁)子序列最大权值(树状数组+dp)
    https://nanti.jisuanke.com/t/17319LetSSbeasequenceofintegerss_{1}s1,s_{2}s2,......,s_{n}snEachintegerisisassociatedwithaweightbythefollowingrules:(1)Ifisisnegative,thenitsweightis00.(2)Ifisisgreaterthanorequalto10......
  • spfa任意两点间最短路径
    #include<iostream>#include<queue>#include<string.h>usingnamespacestd;#defineINF0x3f3f3f3f;constintN=3000;intn,m;intg[N][N],dist[N];boolst[N];queue<int>q;voidspfa(intstart){st[start]=true;dist[s......
  • 用SPFA判断负权图
    #include<bits/stdc++.h>usingnamespacestd;constintN=100010,M=200010,INF=0x3f3f3f3f;#definelllonglonginte[N],ne[N],h[N],w[N],d[N],cnt[N],idx=1;intn,m;boolst[N];//记录是否在队列里voidadd(inta,intb,intc){e[idx]=b,w[idx......
  • 洛谷 P6938 - [ICPC2017 WF]Son of Pipe Stream(网络流)
    见过的最怪的网络流题,没有之一。首先新建超级源点,向\(1,2\)各连\(\infty\)的边。设最大流为\(A\),那么显然最优方案中flutter和water流量之和为\(A\)。先分析一波答案函数。显然,最终答案关于flutter的流量\(x\)的函数\(f(x)=x^a(A-x)^{1-a}\)。求导得\(f'(x)=ax^......
  • 「学习笔记」SPFA 算法的优化
    与其说是SPFA算法的优化,倒不如说是Bellman-Ford算法的优化。栈优化将原本的bfs改为dfs,在寻找负环时可能有着更高效的效率,但是最坏复杂度为指数级别。voiddfs_spfa(intu){ if(fg)return; vis[u]=true; for(pilit:son[u]){ intv=it.first; llw=......
  • SPFA 算法:实现原理及其应用
    一、前言SPFA算法,全称为ShortestPathFasterAlgorithm,是求解单源最短路径问题的一种常用算法,它可以处理有向图或者无向图,边权可以是正数、负数,但是不能有负环。二、SPFA算法1、SPFA算法的基本流程1.初始化首先我们需要起点s到其他顶点的距离初始化为一个很大的值(比如99......
  • 第九届福建省大学生程序设计竞赛-重现赛(感谢承办泉州师范学院) spfa变形
    Xzzisachildwithsevereprocrastinations.Thenewsemesterbegins,Hestillhasalotofhomeworktodo.Now,heneedsyourhelp.Asthebestfriend,youaregoodatmath.So,youwillhelphimdosomemathhomework.NowXzzwantstogotoyourhome.Y......
  • ACM International Collegiate Programming Contest 2014 B SPFA 统计路径
    FloweryTrails!”()”*+,-).%”/)’0”122”1!2”342”522”!22”652”!42”72”72”5!2”!12”622”18!”162”!12”6”7”4”9”3”5”8”1”2”Inordertoattractmorevisitors,themanagerofanationalp......
  • 图的最短路问题(综合详解!!!看这一篇就够了)(spfa-Dijkstra-floyd-模板代码c-)
    文章目录图论:三种最短路及模板模板SPFA算法Floyd算法Dijkstra算法例题与应用反向建边最短路计数1488.最短距离3305.作物杂交4074.铁路与公路图论:三种最短路及模板注意:在这三种算法中我均使用的链式前向星存图,具体方式请看我这篇博客:链式前向星存图详解模板SPFA算法spfa是优化......
  • Codeforces Round #257 (Div. 1)B题Jzzhu and Cities(spfa+slf优化)
    题目地址:http://codeforces.com/contest/450/problem/D这题有重边,需要去重。。sad。当时居然没看见。。这题只要引入一个最短路条数,然后再遍历火车线,如果最短路与火车线长度相等,此时如果最短路条数是1的话,那说明这个最短路就是火车线,不能去掉,如果最短路条数大于1条,说明除了这条火车......