/*
dfs比较好想,就是测试数据的问题,导致在遍历边的时候要倒着遍历才过!
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#define Max 0x3f3f3f3f
using namespace std; struct node{
int D;
int L, T;
node(int D, int L, int T){
this->D=D;
this->L=L;
this->T=T;
}
node(){
}
}; node v[][];
int cnt[];
int vis[]; int maxCost, R, N, S, D, L, T;
int cost, dist; void dfs(int cur, int d, int c){
int i;
if(cur == N){
if(dist>d){
dist=d;
if(cost>c) cost=c;
}
return ;
}
for(i=cnt[cur]-; i>=; --i)
if(!vis[v[cur][i].D] && c+v[cur][i].T<=maxCost && d+v[cur][i].L<dist){
vis[v[cur][i].D]=;
dfs(v[cur][i].D, d+v[cur][i].L, c+v[cur][i].T);
vis[v[cur][i].D]=;
}
} int main(){
while(scanf("%d", &maxCost)!=EOF){
scanf("%d%d", &N, &R);
memset(cnt, , sizeof(cnt));
while(R--){
scanf("%d%d%d%d", &S, &D, &L, &T);
v[S][cnt[S]++]=node(D, L, T);
}
cost=dist=Max;
memset(vis, , sizeof(vis));
dfs(, , );
if(cost<=maxCost)
printf("%d\n", dist);
else printf("-1\n");
}
return ;
}
 /*
spfa + 01背包
dist[next][j]=min(dist[cur][j-v[cur][i].T]+v[cur][i].L) j={v[cur][i].T。。。maxCost}
一维数组表示的是城市节点,二维表示的当前费用
dist[i][j]表示经过i城市,费用为j时总的路径长度!
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#define Max 0x3f3f3f3f
using namespace std; struct node{
int D;
int L, T;
node(int D, int L, int T){
this->D=D;
this->L=L;
this->T=T;
}
node(){
}
}; node v[][];
int cnt[];
int dist[][];
int vis[];
queue<int>q;
int maxCost, R, N, S, D, L, T; int spfa(){
int i;
while(!q.empty()){
int cur = q.front();
q.pop();
vis[cur]=;
for(i=; i<cnt[cur]; ++i){
int next=v[cur][i].D;
for(int j=v[cur][i].T; j<=maxCost; ++j)
if(dist[next][j]>dist[cur][j-v[cur][i].T]+v[cur][i].L){
dist[next][j]=dist[cur][j-v[cur][i].T]+v[cur][i].L;
if(!vis[next]){
vis[next]=;
q.push(next);
}
}
}
}
} void bfs(int cur){
q.push();
memset(dist, 0x3f, sizeof(dist));
for(int i=; i<; ++i)
dist[][i]=;
vis[]=;
spfa();
} int main(){
while(scanf("%d", &maxCost)!=EOF){
scanf("%d%d", &N, &R);
memset(cnt, , sizeof(cnt));
while(R--){
scanf("%d%d%d%d", &S, &D, &L, &T);
v[S][cnt[S]++]=node(D, L, T);
}
memset(vis, , sizeof(vis));
bfs();
int minDist=Max;
for(int i=; i<=maxCost; ++i)
if(minDist>dist[N][i])
minDist=dist[N][i];
if(minDist!=Max)
printf("%d\n", minDist);
else printf("-1\n");
}
return ;
}

poj 1724ROADS(bfs和dfs做法)的更多相关文章

  1. POJ 2243 简单搜索 (DFS BFS A*)

    题目大意:国际象棋给你一个起点和一个终点,按骑士的走法,从起点到终点的最少移动多少次. 求最少明显用bfs,下面给出三种搜索算法程序: // BFS #include<cstdio> #i ...

  2. 题解报告:poj 1426 Find The Multiple(bfs、dfs)

    Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose ...

  3. CodeForces - 510B Fox And Two Dots (bfs或dfs)

    B. Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. 借助leetcode题目来了解BFS和DFS

    广度优先和深度优先搜索 前言 看着这两个搜索的前提的是读者具备图这一数据结构的基本知识,这些可以直接百度一波就了解了.图也像树一样,遍历具有很多的学问在里面,下面我将借用leetcode的题目讲解一下 ...

  5. POJ 1321 棋盘问题 --- DFS

    POJ 1321 题目大意:给定一棋盘,在其棋盘区域放置棋子,需保证每行每列都只有一颗棋子. (注意 .不可放 #可放) 解题思路:利用DFS,从第一行开始依次往下遍历,列是否已经放置棋子用一个数组标 ...

  6. HDU-4607 Park Visit bfs | DP | dfs

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 首先考虑找一条最长链长度k,如果m<=k+1,那么答案就是m.如果m>k+1,那么最 ...

  7. BFS和DFS详解

    BFS和DFS详解以及java实现 前言 图在算法世界中的重要地位是不言而喻的,曾经看到一篇Google的工程师写的一篇<Get that job at Google!>文章中说到面试官问 ...

  8. 算法录 之 BFS和DFS

    说一下BFS和DFS,这是个比较重要的概念,是很多很多算法的基础. 不过在说这个之前需要先说一下图和树,当然这里的图不是自拍的图片了,树也不是能结苹果的树了.这里要说的是图论和数学里面的概念. 以上概 ...

  9. hdu--1026--Ignatius and the Princess I(bfs搜索+dfs(打印路径))

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

随机推荐

  1. java基础1_Java数据类型

    一 . Java的数据类型分为 1.原生数据类型 也叫基本数据类型,分为整形,浮点型,字符型,布尔型.整形有 byte,short,int,long:浮点型有float,double;字符型有 cha ...

  2. 在自定义TableViewCell类里面添加按钮事件触发不了的一些实践

    我的自定义cell上面有5个控件,分别是一个背景的UIImageView,一个专辑的UIImageView(上面加了一个播放的button),一个专辑名字的UIImageView(上面加了显示标题的U ...

  3. QC11客户端安装

    win10使用hp qc11 步骤1:安装vcredist_x86,32位 步骤2:安装浏览器客户端 ALMExplorerAddIn,11版本 可能遇到的问题 1. 出现Initialization ...

  4. 浅谈 facebook .net sdk 应用

    今天看了一篇非常好的文章,就放在这里与大家分享一下,顺便也给自己留一份.这段时间一直在学习MVC,另外如果大家有什么好的建议或者学习的地方,也请告知一下,谢谢. 这篇主要介绍如何应用facebook ...

  5. 在Linux上用自己编译出来的coreclr与donet cli运行asp.net core程序

    先在 github 上签出 coreclr 的源代码,运行 ./build.sh 命令进行编译,编译结果在 coreclr/bin/Product/Linux.x64.Debug/ 文件夹中. 接着签 ...

  6. 【情人节来一发】网站添加QQ客服功能

    今年的元宵节遇到情人节,挺不自量力的,呵呵,开篇给各位讲个段子,早上一美女同学在空间发说说道:“开工大吉 起床啦,卖元宵,卖玫瑰,卖避孕套啦-有木有一起去发财的小伙伴?Let’s go…”,对于此种长 ...

  7. FB引擎系列-之CloudSand

    CloudSand,欲打破之前的集中版本制作的模式, http://code.taobao.org/p/cloudsand包含服务器端代码(php)和客户端代码(unity)   EasyDown的时 ...

  8. Window Ghosting

    最近工作中遇到Window Ghosting这个问题, 感觉挺有意思,这里简单记录下. 在XP时代我们的程序没有响应后只能通过任务管理器强制杀掉,但是Vista之后情况变了, 我们仍然可以拖动失去响应 ...

  9. SQLSERVER取当前月第一天和最后一天

    --本月第一天: select   dateadd(dd,-day(getdate())+1,getdate()) --本月最后一天: SELECT dateadd(ms,-3,DATEADD(mm, ...

  10. spring源码分析(二)Aop

    创建日期:2016.08.19 修改日期:2016.08.20-2016.08.21 交流QQ:992591601 参考资料:<spring源码深度解析>.<spring技术内幕&g ...