【t071】最长路
Time Limit: 1 second
Memory Limit: 128 MB
【问题描述】
设G为有n个顶点的有向无环图,G中各顶点的编号为1到n。设w[i,j]为边的长度。请计算图G中从1到n的最长路径。
【数据范围】
20%的数据,n≤100,m≤1000
40%的数据,n≤1,000,m≤10000
100%的数据,n≤1,500,m≤50000
【输入格式】
第一行有两个整数n和m,表示有n个顶点和m条边,接下来m行中每行输入3个整数a,b,v表示从a点到b点有一条长度为v的边。
【输出格式】
一个整数,即1到n之间的最长路径。 如果1到n之间没连通,输出-1。
Sample Input
2 1
1 2 1
Sample Output
1
Sample Input2
2 0
Sample Output2
-1
【题目链接】:http://noi.qz5z.com/viewtask.asp?id=t071
【题解】
把spfa的松弛操作换一下改成求最长路就好;
一开始把dis数组赋值为255,这样方便最后的输出;
so easy…TAT只能刷水题的蒟蒻.
【完整代码】
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int MAXN = 2000;
int n,m,dis[MAXN];
vector <int> a[MAXN],w[MAXN];
queue <int> dl;
bool inque[MAXN];
int main()
{
//freopen("F:\\rush.txt","r",stdin);
scanf("%d%d",&n,&m);
for (int i = 1;i <= m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
a[x].push_back(y);
w[x].push_back(z);
}
memset(dis,255,sizeof(dis));
dis[1] = 0;
inque[1] = true;
dl.push(1);
while (!dl.empty())
{
int x= dl.front();
inque[x] = false;
dl.pop();
int len = a[x].size();
for (int i = 0;i <= len-1;i++)
{
int y= a[x][i],c = w[x][i];
if (dis[y] < dis[x]+c)
{
dis[y] = dis[x] +c;
if (!inque[y])
{
inque[y] = true;
dl.push(y);
}
}
}
}
printf("%d\n",dis[n]);
return 0;
}
【t071】最长路的更多相关文章
- spfa求最长路
http://poj.org/problem?id=1932 spfa求最长路,判断dist[n] > 0,需要注意的是有正环存在,如果有环存在,那么就要判断这个环上的某一点是否能够到达n点,如 ...
- zoj 3795 Grouping tarjan缩点 + DGA上的最长路
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit Status Practic ...
- 寒冰王座(DGA最长路/完全背包)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- 【HDU3721】枚举+最长路
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3721 题意:给你一颗n个节点n-1条边的树,每条边都有一个权值,现在让你任意移动一条边然后把这条边连接 ...
- [USACO2003][poj2138]Travel Games(dp/最长路)
http://poj.org/problem?id=2138 题意:给你一些单词和初始单词,在初始单词的任意位置你可以加任意一个字母,使得这个新单词在给的单词中有所出现,然后在这样不断迭代下去,让你求 ...
- hiho #1050 : 树中的最长路 树的直径
#1050 : 树中的最长路 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中, ...
- NYOJ16 矩形嵌套(DAG最长路)
矩形嵌套 紫书P262 这是有向无环图DAG(Directed Acyclic Graph)上的动态规划,是DAG最长路问题 [题目链接]NYOJ16-矩形嵌套 [题目类型]DAG上的dp & ...
- 中南大学oj 1317 Find the max Link 边权可以为负的树上最长路 树形dp 不能两遍dfs
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1317经典问题:树上最长路,边权可以为负值的,树形dp,不能用两边dfs.反例:5 41 2 22 ...
- zoj 3088 Easter Holidays(最长路+最短路+打印路径)
Scandinavians often make vacation during the Easter holidays in the largest ski resort Are. Are prov ...
随机推荐
- UVA - 590Always on the run(递推)
题目:UVA - 590Always on the run(递推) 题目大意:有一个小偷如今在计划着逃跑的路线,可是又想省机票费. 他刚開始在城市1,必须K天都在这N个城市里跑来跑去.最后一天达到城市 ...
- iOS 中使用 XIB 自定义cell的两种方法以及编译出现常见 的错误 (xcode6.0之后)
一. 注册cell 1.创建自定义cell并勾选 xib :(勾选xib就会自动生成与cell文件关联的xib) 2.在 tableViewController里注册自定义Cell (或者遵守tabl ...
- 在 Windows 10 x64 上安装及使用 ab 工具的流程
本文转自:www.shuijingwanwq.com/2017/04/18/1568/ 1.基于AB测试工具进行高并发情形下的模拟测试,打开:http://httpd.apache.org/docs/ ...
- [React Intl] Install and Configure the Entry Point of react-intl
We’ll install react-intl, then add it to the mounting point of our React app. Then, we’ll use react- ...
- Linux下交叉编译gdb,gdbserver+gdb的使用以及通过gdb调试core文件
交叉编译gdb和gdbserver 1.下载gdb:下载地址为:http://ftp.gnu.org/gnu/gdb/按照一般的想法,最新版本越好,因此下载7.2这个版本.当然,凡事无绝对.我们以gd ...
- 29、从零写USB摄像头驱动之通过urb接受数据后上报数据是函数中fid的作用
原因分析如下: 视频数据是由一帧一帧数据组成,为了防止数据错乱,会给每一帧数据分配一个frameid,从第0帧开始,接着是第1帧,接着又是第0帧这样交错进行的,对usb摄像头来说每一帧数据来源于多个包 ...
- 【19.77%】【codeforces 570D】Tree Requests
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- [MobX] MobX fundamentals: deriving computed values and managing side effects with reactions
Derivations form the backbone of MobX and come in two flavors: computed values are values that can b ...
- 7 Best jQuery & JavaScript PDF Viewer plugin with examples
In this Post we are providing best jQuery PDF viewer plugin & tutorial with examples.Due to popu ...
- thinkphp5项目--练手--企业单车网站(九)(友情链接)
thinkphp5项目--练手--企业单车网站(九)(友情链接) 项目地址 fry404006308/BicycleEnterpriseWebsite: Bicycle Enterprise Webs ...