【POJ - 1724 】ROADS (带限制的最短路 或 dfs 或 A*算法,双权值)

题干:

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 number of coins). 
Bob and Alice used to live in the city 1. After noticing that Alice was cheating in the card game they liked to play, Bob broke up with her and decided to move away - to the city N. He wants to get there as quickly as possible, but he is short on cash. 

We want to help Bob to find the shortest path from the city 1 to the city N that he can afford with the amount of money he has. 

Input

The first line of the input contains the integer K, 0 <= K <= 10000, maximum number of coins that Bob can spend on his way. 
The second line contains the integer N, 2 <= N <= 100, the total number of cities. 

The third line contains the integer R, 1 <= R <= 10000, the total number of roads. 

Each of the following R lines describes one road by specifying integers S, D, L and T separated by single blank characters : 

  • S is the source city, 1 <= S <= N 
  • D is the destination city, 1 <= D <= N 
  • L is the road length, 1 <= L <= 100 
  • T is the toll (expressed in the number of coins), 0 <= T <=100


Notice that different roads may have the same source and destination cities.

Output

The first and the only line of the output should contain the total length of the shortest path from the city 1 to the city N whose total toll is less than or equal K coins. 
If such path does not exist, only number -1 should be written to the output. 

Sample Input

5
6
7
1 2 2 3
2 4 3 3
3 4 2 4
1 3 4 1
4 6 2 1
3 5 2 0
5 4 3 2

Sample Output

11

题目大意:

   题意第一行给了一个k,代表你有k的钱数,下一行有一个n,代表n个点,然后一个m,代表m条边,然后接下来m行,每行有四个数,分别代表起点、终点、路径长度和要花费的钱数,题目想问在花的钱不超过k的情况下,从1---n的最短路径是多少。

解题报告:

    注意cost和length不能直接存到二维数组中,然后用min来去重边,,因为可能一个大一个小,emmm对于这种两个权值的还是要注意啊。。。

AC代码1:(94ms)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 500 + 5;
const int INF = 0x3f3f3f3f;
int k,n,m;
struct Node {int v;int w,c;int ne;Node(){}Node(int v,int w,int c):v(v),w(w),c(c){}
}e[MAX*MAX];
int head[MAX];
bool vis[MAX];
int ans = INF,tot;
void add(int u,int v,int w,int c) {e[++tot].v = v;e[tot].w = w;e[tot].c = c;e[tot].ne = head[u];head[u] = tot;
}
void dfs(int cur,int c,int w) {//c花费 w路程 if(c > k || w > ans) return;if(cur == n) {ans = min(ans,w);return ;}for(int i = head[cur]; i!=-1; i=e[i].ne) {Node v = e[i];if(vis[v.v]) continue;vis[v.v] = 1;dfs(v.v,c+v.c,w+v.w);vis[v.v] = 0;}
}
int main()
{cin>>k>>n>>m;int u,v,w,c;memset(head,-1,sizeof head);for(int i = 1; i<=m; i++) {scanf("%d%d%d%d",&u,&v,&w,&c);add(u,v,w,c);}dfs(1,0,0);if(ans == INF) puts("-1");else printf("%d\n",ans);return 0 ;}

AC代码2:(32ms)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 500 + 5;
const int INF = 0x3f3f3f3f;
int k,n,m;
struct Node {int v;int dis,c;int ne;Node() {}Node(int v,int w,int c):v(v),dis(dis),c(c) {}
} e[MAX*MAX];
struct Point {int id,dis,cost;bool friend operator < (Point a,Point b) {return a.dis>b.dis;}
};
//vector<Node> vv[MAX];
int head[MAX];
bool vis[MAX];
int ans = INF,tot;
void add(int u,int v,int w,int c) {e[++tot].v = v;e[tot].dis = w;e[tot].c = c;e[tot].ne = head[u];head[u] = tot;
}
ll dp[105][10005];//dp[i][j] : 到i号城市,花费为j,的最短路。
void Dijkstra() {for(int i=1; i<=n; i++)for(int j=0; j<=k; j++)dp[i][j]=INF;dp[1][0]=0;priority_queue<Point>pq;Point now,cur;cur.id=1;cur.cost=0;cur.dis=0;pq.push(cur);while(!pq.empty()) {cur=pq.top();pq.pop();if(cur.id==n) {printf("%d\n",cur.dis);return;}for(int i=head[cur.id]; i!=-1; i=e[i].ne) {int cost=cur.cost+e[i].c;if(cost>k)continue;if(dp[e[i].v][cost]>cur.dis+e[i].dis) { dp[e[i].v][cost]=cur.dis+e[i].dis;now.id=e[i].v;now.cost=cost;now.dis=dp[e[i].v][cost];pq.push(now);}}}puts("-1");
}
int main() {cin>>k>>n>>m;int u,v,w,c;memset(head,-1,sizeof head);for(int i = 1; i<=m; i++) {scanf("%d%d%d%d",&u,&v,&w,&c);add(u,v,w,c);}Dijkstra();return 0 ;
}

写Dijkstra的时候就不需要if(vis[cur.id]) continue;了。。(其实这一句在一般的Dijkstra中也可以不写,只是统计路径条数的时候必须要写上这个,不然就算重复了(比如那道qduoj的题))

AC代码3:(47 - 32ms)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 500 + 5;
const int INF = 0x3f3f3f3f;
int k,n,m;
struct Node {int v;int dis,c;int ne;Node() {}Node(int v,int w,int c):v(v),dis(dis),c(c) {}
} e[MAX*MAX];
struct Point {int id,dis,cost;bool friend operator < (Point a,Point b) {return a.dis>b.dis;}
};
//vector<Node> vv[MAX];
int head[MAX];
bool vis[MAX];
int ans = INF,tot;
void add(int u,int v,int w,int c) {e[++tot].v = v;e[tot].dis = w;e[tot].c = c;e[tot].ne = head[u];head[u] = tot;
}
ll dp[105][10005];//dp[i][j] : 到i号城市,花费为j,的最短路。
int bfs() {priority_queue<Point>pq;Point now,cur;cur.id=1;cur.cost=0;cur.dis=0;pq.push(cur);while(!pq.empty()) {cur=pq.top(); pq.pop();if(cur.id==n) {if(cur.cost <= k) return cur.dis;}for(int i=head[cur.id]; i!=-1; i=e[i].ne) {int nowcost=cur.cost+e[i].c;if(nowcost>k)continue;now.id=e[i].v;now.cost=nowcost;now.dis=cur.dis + e[i].dis;pq.push(now);}}puts("-1");
}
int main() {cin>>k>>n>>m;int u,v,w,c;memset(head,-1,sizeof head);for(int i = 1; i<=m; i++) {scanf("%d%d%d%d",&u,&v,&w,&c);add(u,v,w,c);}printf("%d\n",bfs());return 0 ;
}

总结:

  对于poj我还能说什么,,换上vector立刻tle。。

 

AC代码4:(A*算法,0ms)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 150 + 5;
const int INF = 0x3f3f3f3f;
int k,n,m;
struct Node {int v;int dis,c;int ne;Node() {}Node(int v,int w,int c):v(v),dis(dis),c(c) {}
} e[MAX*MAX],r[MAX*MAX];
struct Point {int id,dis,cost;bool friend operator < (Point a,Point b) {return a.dis>b.dis;}
};
//vector<Node> vv[MAX];
int head[MAX],read[MAX];
bool vis[MAX];
int Dis[MAX];
int ans = INF,tote,totr;
void add(int u,int v,int w,int c) {e[++tote].v = v;e[tote].dis = w;e[tote].c = c;e[tote].ne = head[u];head[u] = tote;
}
void add2(int u,int v,int w,int c) {r[++totr].v = v;r[totr].dis = w;r[totr].c = c;r[totr].ne = read[u];read[u] = totr;
}
void Dijkstra() {memset(Dis,INF,sizeof Dis);priority_queue<Point>pq;Point now,cur;cur.id=n;cur.cost=0;cur.dis=0;Dis[n] = 0;pq.push(cur);while(pq.size()) {cur=pq.top(); pq.pop();if(vis[cur.id]) continue;vis[cur.id] = 1;for(int i=read[cur.id]; i!=-1; i=r[i].ne) {int cost=cur.cost+r[i].c;
//			if(cost>k)continue;因为就是求标准的最短路,所以这个就没啥用了先 if(Dis[r[i].v]>cur.dis+r[i].dis) { //松弛条件Dis[r[i].v]=cur.dis+r[i].dis;now.id=r[i].v;now.cost=cost;now.dis=Dis[r[i].v];pq.push(now);}}}
}
struct A {int id,dis,c;A(){};A(int id,int dis,int c):id(id),dis(dis),c(c){}bool friend operator < (A a,A b) {return a.dis + Dis[a.id] > b.dis + Dis[b.id];}
};
int Astar(int st,int ed) {priority_queue< A > pq;pq.push(A(st,0,0));while(pq.size()) {A cur = pq.top();pq.pop();if(cur.id == ed) return cur.dis;for(int i = head[cur.id]; i != -1; i = e[i].ne) {if(cur.c + e[i].c <= k) pq.push(A(e[i].v,cur.dis + e[i].dis,cur.c + e[i].c));}}return -1;
}
int main() {cin>>k>>n>>m;int u,v,w,c;memset(head,-1,sizeof head);memset(read,-1,sizeof read);for(int i = 1; i<=m; i++) {scanf("%d%d%d%d",&u,&v,&w,&c);add(u,v,w,c);add2(v,u,w,c);}Dijkstra();printf("%d\n",Astar(1,n));return 0 ;
}

总结:

  其实这个代码求最短路的那个Point结构体都不需要带上cost这个参数,,,因为没啥用。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440839.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mysql 5.7.20 win64_Win10下MySQL5.7.20 Mysql(64位)解压版安装及bug修复

2、解压到某一文件夹&#xff0c;如“C:\Program Files\MySQL\mysql-5.7.20-winx64”3、添加环境变量(系统变量)&#xff1a;变量名&#xff1a;MYSQL_HOME变量值&#xff1a;C:\Program Files\MySQL\mysql-5.7.20-winx64&#xff1b;在系统变量path原有值后添加路径&#xff1…

【CodeForces - 474D】Flowers (线性dp)

题干&#xff1a; We saw the little game Marmot made for Moles lunch. Now its Marmots dinner time and, as we all know, Marmot eats flowers. At every dinner he eats some red and white flowers. Therefore a dinner can be represented as a sequence of several f…

mysql事务顺序重排_MySQL事务处理及字符集和校对顺序

一、事务处理 事务处理&#xff1a;是一种机制&#xff0c;管理必须成批执行的MySQL操作&#xff0c;以保证数据库不包含不完整的操作结果。用来维护数据库的完整性。利用事务处理&#xff0c;可以保证一组操作不会中途停止&#xff0c;或作为整体执行或完全不执行(除非明确指示…

mysql权重怎么配置_mysql如何按权重查询数据啊?

楼上的回答全都会错意了&#xff0c;题主意思是根据权重设定随机几率&#xff0c;例如 A 的权重为10&#xff0c;B 的权重为 5&#xff0c;这个时候随机出现 A 的几率要比出现 B 的几率高。你可以试试这个备选方案。就是先取出权重列表再去根据权重随机出来的那个权重值&#x…

【计蒜客 - 蓝桥训练】修建公路(贪心,或运算,dp)

题干&#xff1a; 蒜头国有 nn 座城市&#xff0c;编号分别为 0,1,2,3,\ldots,n-10,1,2,3,…,n−1。编号为 xx 和 yy 的两座城市之间如果要修高速公路&#xff0c;必须花费 x|yx∣y 个金币&#xff0c;其中|表示二进制按位或。 吝啬的国王想要花最少的价格修建高速公路&#…

【计蒜客 - 蓝桥训练】阶乘位数(数学,对数运算,求阶乘位数)

题干&#xff1a; 蒜头君对阶乘产生了兴趣&#xff0c;他列出了前 1010 个正整数的阶乘以及对应位数的表&#xff1a; nnn!n!位数111221361424251203672037504048403205936288061036288007 对于蒜头君来说&#xff0c;再往后就很难计算了。他试图寻找阶乘位数的规律&#xff…

mysql win10 优化设置_windows10如何优化?系统优化设置方法

windows10如何优化&#xff1f;&#xff0c;高系统效率&#xff0c;尽可能提高运行速度&#xff0c;是我们关心的问题。以下是电脑系统优化设置的4种方法&#xff0c;能够有效的提高系统的使用效率和优化系统管理&#xff0c;我们一起来看看吧&#xff01;第一招&#xff1a;删…

【牛客 - 368D】动态连通块(并查集+bitset优化)

题干&#xff1a; 小T有n个点&#xff0c;每个点可能是黑色的&#xff0c;可能是白色的。 小T对这张图的定义了白连通块和黑连通块&#xff1a; 白连通块&#xff1a;图中一个点集V&#xff0c;若满足所有点都是白点&#xff0c;并且V中任意两点都可以只经过V中的点互相到达&a…

mysql 执行计划extra_mysql执行计划explain type和extra

mysql执行计划&#xff0c;搞定type和extra就能优化大部分sql了。type为主&#xff0c;extra为辅。type&#xff1a;system表只有一行&#xff0c;MyISAM引擎。const常量连接&#xff0c;表最多只有一行匹配&#xff0c;通用用于主键或者唯一索引比较时eq_ref每次与之前的表合并…

【计蒜客 - 蓝桥训练】欧拉函数(数学,数论,模板)

题干&#xff1a; 在数论中&#xff0c;对正整数 nn&#xff0c;欧拉函数 \varphi (n)φ(n) 是小于等于 nn 的正整数中与 nn 互质的数的数目。 例如 \varphi (12)4φ(12)4&#xff0c;因为 1,5,7,111,5,7,11 均和 1212 互质。 代码框中的代码是一种求欧拉函数的实现&#xf…

spark中读取json_【spark】文件读写和JSON数据解析

1.读文件通过 sc.textFile(“file://")方法来读取文件到rdd中。val lines sc.textFile("file://")//文件地址或者HDFS文件路径本地地址"file:///home/hadoop/spark-1.6.0-bin-hadoop2.6/examples/src/main/resources/people.json"HDFS文件地址"…

【牛客 - 370B】Rinne Loves Graph(分层图最短路 或 最短路dp)

题干&#xff1a; Island 发生了一场暴乱&#xff01;现在 Rinne 要和 Setsuna 立马到地上世界去。 众所周知&#xff1a;Island 是有一些奇怪的城镇和道路构成的&#xff08;题目需要&#xff0c;游戏党勿喷&#xff09;&#xff0c;有些城镇之间用双向道路连接起来了&…

设python中有模块m、如果希望同时导入m中的所有成员_python-模块

先做几个练习题练习计算一个四乘四矩阵的所有元素的和&#xff0c;以及对角线之和#encodingutf-8a[[1,2,3,4],[2,5,2,3],[1,5,3,2],[5,3,2,5]]#encodingutf-8a[[1,2,3,4],[2,5,2,3],[1,5,3,2],[5,3,2,5]]total_sum0diagonal_sum0‘‘‘for i in a:print "i:",ifor j …

【牛客 - 370F】Rinne Loves Edges(树,统计dp)

题干&#xff1a; Rinne 最近了解了如何快速维护可支持插入边删除边的图&#xff0c;并且高效的回答一下奇妙的询问。 她现在拿到了一个 n 个节点 m 条边的无向连通图&#xff0c;每条边有一个边权 wiwi 现在她想玩一个游戏&#xff1a;选取一个 “重要点” S&#xff0c;然…

java 单例 饿汉式_Java-单例设计模式(懒汉与饿汉)

单例设计模式保证一个类在内存中只能有一个对象。思路&#xff1a;1)如果其他程序能够随意用 new 创建该类对象&#xff0c;那么就无法控制个数。因此&#xff0c;不让其他程序用 new 创建该类的对象。2)既然不让其他程序 new 该类对象&#xff0c;那么该类在自己内部就要创建一…

【HYSBZ - 2763 】飞行路线 (分层图最短路,最短路dp)

题干&#xff1a; Alice和Bob现在要乘飞机旅行&#xff0c;他们选择了一家相对便宜的航空公司。该航空公司一共在n个城市设有业务&#xff0c;设这些城市分别标记为0到n-1&#xff0c;一共有m种航线&#xff0c;每种航线连接两个城市&#xff0c;并且航线有一定的价格。Alice和…

java 命令行 编译 jar文件_用命令行编译java并生成可执行的jar包

如果想用java编写一个可视化小程序&#xff0c;碰巧手头没有IDE的话&#xff0c;可以用命令行来完成编译、打包等工作。拿自己编写的“java记事本”为例&#xff0c;介绍一下这个过程&#xff1a;1.编写源代码。编写源文件&#xff1a;NotePad.java并保存&#xff0c;例如&…

【牛客 - 369A】小D的剧场(线性dp)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/369/A 来源&#xff1a;牛客网 题目描述 "我明白。" 作为这命运剧场永远的观众&#xff0c;小D一直注视着这片星光璀璨的舞台&#xff0c;舞台上&#xff0c;少女们的身姿演绎出了一幕幕…

java list遍历添加元素_java遍历List过程中添加和删除元素的问题

遍历元素最常见的三种方法&#xff1a;//第三种遍历【利用迭代器】private static void loopList3(List strList) {Iterator itr strList.iterator();while (itr.hasNext()){String tmp itr.next();if("000".equals(tmp)){itr.remove();}else{System.out.println(t…

【蓝桥杯官网试题 - 算法提高 】求最大值 (dp,0-1背包)

题干&#xff1a; 问题描述 给n个有序整数对ai bi&#xff0c;你需要选择一些整数对 使得所有你选定的数的aibi的和最大。并且要求你选定的数对的ai之和非负&#xff0c;bi之和非负。 输入格式 输入的第一行为n&#xff0c;数对的个数   以下n行每行两个整数 ai bi 输出格…