POJ 1724 ROADS
来源:互联网 发布:算法入门怎么学 编辑:程序博客网 时间:2024/04/29 09:47
题目链接:点击打开链接
题目大意:有n个点,给你r条边,再给你k块钱,r行数据中每行4个整数S,D,L,T代表从S到D点的长度为L,需要花费为T,问你从1到n点在k块钱足够的情况下最短路长是多少?
题目解析:简单的搜索,和POJ3411的做法差不多,记得如果当前状态已经大于答案了或者钱已经不够了就要及时return,没有必要再继续搜下去,然后就没啥啦。
#include <algorithm>#include <iostream>#include <numeric>#include <cstring>#include <iomanip>#include <string>#include <vector>#include <cstdio>#include <queue>#include <stack>#include <cmath>#include <map>#include <set>#define LL long long#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const int m = 100007;const double esp = 1e-6;const double PI = 3.14159265359;const int INF = 0x3f3f3f3f;using namespace std;const int maxn = 10005;struct node{ int v,len,toll,next;}Map[maxn];int n,r,s,d,l,t,tot;int head[105];int vis[105],ans;void add_edge(){ Map[tot].v = d; Map[tot].len = l; Map[tot].toll = t; Map[tot].next = head[s]; head[s] = tot; tot += 1;}void dfs(int fr,int toll,int dis){ if(dis > ans) return ; if(fr == n && toll >= 0){ ans = min(ans,dis); return ; } for(int i=head[fr];i!=-1;i=Map[i].next){ if(!vis[Map[i].v] && toll >= Map[i].toll){ vis[Map[i].v] = true; dfs(Map[i].v,toll-Map[i].toll,dis+Map[i].len); vis[Map[i].v] = false; } } return ;}int main(){ int k; while(~scanf("%d %d %d",&k,&n,&r)){ memset(Map,INF,sizeof(Map)); memset(head,-1,sizeof(head)); tot = 0; for(int i=0;i<r;i++){ scanf("%d %d %d %d",&s,&d,&l,&t); add_edge(); } memset(vis,false,sizeof(vis)); ans = INF; dfs(1,k,0); printf("%d\n",ans==INF?-1:ans); } return 0;}
0 0
- POJ 1724 ROADS
- POJ 1724 ROADS
- poj 1724 ROADS
- Poj 1724 ROADS
- POJ-1724-ROADS
- poj 1724 ROADS
- POJ 1724 ROADS
- POJ-1724-ROADS
- POJ 1724 ROADS (bfs)
- poj 1724 ROADS
- POJ 1724 ROADS
- POJ 1724 ROADS
- POJ 1724 ROADS
- POJ 1724 ROADS
- POJ 1724--ROADS
- poj 1724 ROADS
- poj 1724 ROADS
- POJ-1724 ROADS
- JZOJ4710. 【NOIP2016提高A组模拟8.17】Value
- "传输速率每秒100G "
- Intellij Idea&Android Studio中iml文件、modules.xml文件作用
- POJ 2001 Shortest Prefixes (zidiansh)
- 安卓中的存储(SQLite)
- POJ 1724 ROADS
- 两种异步处理的源码分析(一):handler,message,looper
- 1041. 考试座位号(15)
- POJ 2418 Hardwood Species (字典树 || map)
- 2016/8/17总结
- Matlab问答记day1
- stm32开发板画错导致的jlink问题
- CRM项目总结
- MySql(3)------SQL基础之DML(Data Manipulation Language),不包含select语句