题目链接 n个节点, m条边, 一开始有K这么多的钱, 每条边有len, cost两个属性, 求1到n的最短距离, 花费要小于k. dis数组开成二维的, dis[u][cost]表示到达u花费为cost的最短路径, 然后dij+堆优化. 路是单向的.. #include <iostream> #include <vector> #include <cstdio> #include <cstring> #include <algorithm> #…
POJ 1724 ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12766   Accepted: 4722 Description N cities named with numbers 1 ... N are connected with one-way roads. Each road has two parameters associated with it : the road length and…
http://poj.org/problem?id=1724 题意:最短路的模板,不过每条边加上一个费用,要求总费用不超过k 题解:不能用dijkstra ,直接暴力,dfs维护len和cost. 普通的剪枝:如果当前的cost大于k直接跳出,如果当前的len大于minlen(目前的最优解),跳出. 另一个剪枝:维护花费一定费用 到达某个点 的最短路minL[v][cost],如果当前的len大于L,则跳出. ac代码: #define _CRT_SECURE_NO_WARNINGS #incl…
题目链接: https://cn.vjudge.net/problem/POJ-1724 N cities named with numbers 1 ... N are connected with one-way roads. Each road has two parameters associated with it : the road length and the toll that needs to be paid for the road (expressed in the num…
题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变的SPFA啊,链接.还有一个神用了好几种方法分析,链接 . 用优先队列控制长度,保证每次加的都是最短的,每次从队列中取元素,沿着取出来的点往下找,如果费用比K少再加入队列,否则不加,这样可以省时间. #include <stdio.h> #include <string.h> #inc…
n个点m条边的有向图,每条边有距离跟花费两个参数,求1->n花费在K以内的最短路. 直接优先队列bfs暴力搞就行了,100*10000个状态而已.节点扩充的时候,dp[i][j]表示到达第i点花费为j时的最短路.没加优化16ms过,不知道discuss里面说bfs超时是怎么回事.... #include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #include&…
Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible. Farmer John's field ha…
一道写法多样的题,很具有启发性. 具体参考:http://www.cnblogs.com/scau20110726/archive/2013/04/28/3050178.html http://blog.csdn.net/lyy289065406/article/details/6692382…
I. Move Between Numbers   time limit per test 2.0 s memory limit per test 256 MB input standard input output standard output You are given n magical numbers a1, a2, ..., an, such that the length of each of these numbers is 20 digits. You can move fro…
畅通project续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 28578    Accepted Submission(s): 10382 Problem Description 某省自从实行了非常多年的畅通project计划后.最终修建了非常多路. 只是路多了也不好,每次要从一个城镇到还有一个城镇时.都有很多种道路方案能够选择…
题目链接 \(Description\) 给定一张有向图,求哪些边一定在最短路上.对于不一定在最短路上的边,输出最少需要将其边权改变多少,才能使其一定在最短路上(边权必须为正,若仍不行输出NO). \(Solution\) 正反跑两遍Dijkstra.一条边\((u,v,w)\)在最短路上当且仅当\(dis[S][u]+dis[v][T]+w=dis[S][T]\). 一定在最短路上则满足,从\(S\)走最短路到\(u\)的方案数 * 从\(v\)走最短路到\(T\)的方案数 = 从\(S\)到…
题意:给你N个城市和M条路和K块钱,每条路有话费,问你从1走到N的在K块钱内所能走的最短距离是多少 链接:http://poj.org/problem?id=1724 直接dfs搜一遍就是 代码: #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <stdlib.h> #include <vector> #i…
题意:由n个牧场,编号1到n.每个牧场有一头牛.现在在牧场x举办party,每头牛都去参加,然后再回到自己的牧场.牧场之间会有一些单向的路.每头牛都会让自己往返的路程最短.问所有牛当中最长的往返路程是多少. 思路:n最多到1000,floyd肯定超时.可以这样做,把图中所有的边先存起来,然后第一次用dijkstra求出以x为源点到每个点的最短距离.该最短距离为每头牛回家时的最短距离.然后建个新的图,将之前存的边反向加入图中.如之前有条从5到8距离为2的路,则此时向图中添加的边为从8到5距离为2的…
题目链接:http://poj.org/problem?id=1724 题目意思:给出一个含有N个点(编号从1~N).R条边的有向图.Bob 有 K 那么多的金钱,需要找一条从顶点1到顶点N的路径(每条边需要一定的花费),前提是这个总花费  <= K. 首先这里很感谢 yunyouxi0 ,也就是我们的ACM队长啦~~~,他一下子指出了我的错误——存储重边的错误.这条题卑鄙的地方是,有重边,discuss 中的数据过了也不一定会AC啦.大家不妨试试这组数据(队长深情奉献^_^) 2 2 2 1…
这道题我拖了半年,,,终于写出来了 思路: 先反向建边 从终点做一次最短路 ->这是估价函数h(x) 再正常建边,从起点搜一遍 (priority_queue(h(x)+g(x))) g(x)是已经走过的.. 思路比较简单,,, 但是我总是MLE 原因:写挫了-- 刷了三页- - //By SiriusRen #include <queue> #include <cstdio> #include <cstring> using namespace std; #de…
ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Description N cities named with numbers 1 ... N are connected with one-way roads. Each road has two parameters associated with it : the road length and the toll…
题意:有R条路,每条路都有一定的路长和花费,问在总的花费小于一定的值的情况下,从1到N的最短路程         注意:这里两点之间单向边,且可能存在很多条路,所以只能用邻接表存储.思路:用dijkstra,但是每次判断一个元素是否能进入队列,并不是源点到它的距离被更新了才入队, 而是只要满足从源点到该点总的路费小于给定的值,都可入队. 每次从优先级队列中取出路程最小(如果路程相同,取花费最小)的点. 当N点被取出来时,直接结束 #include <iostream> #include <…
不写普通模板了,还是需要优先队列优化的昂 #include<stdio.h> //基本需要的头文件 #include<string.h> #include<queue> #include<algorithm> #include<vector> using namespace std; typedef pair<int,int> pii; const int INF=0x3f3f3f3f; ; ; ],val[maxm<<]…
http://acm.hdu.edu.cn/showproblem.php?pid=2544 #include<iostream> #include<queue> #include<functional> #include<algorithm> #include<cstring> #include<vector> using namespace std; ; const int INF = 1e7; typedef pair<i…
题目传送门 题意:问从1到n的最短路径,同时满足花费总值小于等于k 分析:深搜+剪枝,如果之前走过该点或者此时的路劲长度大于最小值就不进行搜索. /************************************************ * Author :Running_Time * Created Time :2015/11/11 星期三 10:14:14 * File Name :POJ_1724.cpp **************************************…
题目链接 用STL实现超时了,用普通队列500+,看到spfa,反应太迟钝了. #include <cstring> #include <cstdio> #include <string> #include <iostream> #include <algorithm> #include <vector> #include <queue> using namespace std; #define INF 0x7ffffff…
1.裸题 hdu2544 http://acm.hdu.edu.cn/showproblem.php?pid=2544 Way1: 好像不对 #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <stdbool.h> #include <set> #include <vector> #include <ma…
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12436 Accepted: 4591 Description N cities named with numbers 1 - N are connected with one-way roads. Each road has two parameters associated with it : the road length and the toll that…
### POJ 1724 题目链接 ### 题目大意: 给你 N 个点 ,M 条有向路,走每条路需要花费 C 元,这段路的长度为 L . 给你 K 元,问你能否从 1 走到 N 点且花费不超过 K 元.如果可以,输出出最短距离,否则输出 -1 . 显然分层图最短路,这里 dist[i][j] 表示从 1 到 i 点 且 所剩钱数为 j 时的最短路,然后跑一遍 dijkstra 即可. PS:在优先队列先到达 N 点的即为答案,可以直接返回,不需要等队列走完再 O(N)找最小值,时间会很快(这里还…
题目链接:http://poj.org/problem?id=1511 就是求从起点到其他点的最短距离加上其他点到起点的最短距离的和 , 注意路是单向的. 因为点和边很多, 所以用dijkstra优先队列的做法. 起点到其他点的最短距离之和就是dij一下 . 要求其他点到起点的最短距离的话就是把原先边的方向反向一下,然后再求起点到其他点的最短距离之和 , 同样dij一下. 代码如下: #include <iostream> #include <cstdio> #include &l…
刚开始想复杂了,一直做不出来,,,其实就是两遍dijkstra+优先队列(其实就是板子题,只要能有个好的板子,剩下的都不是事),做出来感觉好简单...... 题意:有n个车站和n个志愿者,早上每个志愿者去一个站点,晚上回去,问最少的开销是多少.是一个有向图 先一遍dijkstra求出早上的开销,在把车站倒过来及1到4变成4到1,然后再一遍dijkstra求出晚上的开销. 不要用memset去初始化为INF, 本题如果用memset会wa,记得开long long: 1 #include<cstd…
传送门:地铁 思路:拆点,最短路:拆点比较复杂,所以对边进行最短路,spfa会tle,所以改用Dijkstra(优先队列优化) 模板 /************************************************************** Problem: User: youmi Language: C++ Result: Accepted Time: Memory: *****************************************************…
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4829 Patricia is an excellent software developer, but, as every brilliant person, she has some strange quirks.One of those is that everything she d…
一下午都在学最短路dijkstra算法,总算是优化到了我能达到的水平的最快水准,然后列举一下我的优化历史,顺便总结总结 最朴素算法: 邻接矩阵存边+贪心||dp思想,几乎纯暴力,luoguTLE+MLE, 算优点??:但好写好想,比下面的代码短了差不多一半. #include <iostream> #include <cstdio> #include <algorithm> using namespace std; int main() { ][],point,i,j,…
题意: n个点,m条边,问从1走到n的最短路,其中有K次机会可以让一条路的权值变成0.1≤N≤10000;1≤M≤500000;1≤K≤20 题解: 拆点,一个点拆成K个,分别表示到了这个点时还有多少次机会.(x,k)-->(y,k-1),cost=0 或 (x,k)-->(y,k),cost=a[i].d;这题数据比较大, 需要很多优化.(应该只是蒟蒻我才需要这么多优化..)1.不用spfa(时间复杂度不稳定),用dijkstra+优先队列优化2.拆点不拆边.g[i]表示i这个点是由谁拆分出…