/*
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. ubuntu下面mysql,通过载入txt文件初始化数据表

    环境:ubuntu12.04   mysql(通过apt安装) (1)根据数据表中的属性列,对应在txt中构造记录(一行对应一条记录),不同属性之间通过tab键(以/root目录下构建的init.tx ...

  2. 关于HIVE的数据模型介绍

    (1)Hive数据库 类似传统数据库的DataBase,在第三方数据库里实际是一张表.简单示例命令行 hive > create database test_database; (2)内部表 H ...

  3. Android 适配2

    Android AutoLayout全新的适配方式 堪称适配终结者 转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/4999094 ...

  4. 美帝的emal to message gateway

    Provider Email to SMS Address Format AllTel number@text.wireless.alltel.com AT&T number@txt.att. ...

  5. toolControls添加工具项

    最近参考Arcengine的Samples做的功能,虽然简单,但是示例代码的确体现出了很好的封装性,值得学习,效果图如下: 闲话休絮,直入正题: 一.首先建立工具类,实现IMenuDef接口 clas ...

  6. spark on centos6.5 安装

    第一步在centos 6.5上安装java 运行环境 http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-18 ...

  7. [ASE][Daily Scrum]11.28

    昨天基本上已经完成了sprint2的task, 现在剩下一些bug还需要来修正, 然后我正式加入码代码的大军啦~ Shilin Liu  显示聊天框 Zhao Li     搭建聊天服务器 Yimin ...

  8. WebAPI的一种单元测试方案

    大家是如何对webApi写测试的呢? 1.利用Fiddler直接做请求,观察response的内容. 2.利用Httpclient做请求,断言response的内容. 3.直接调用webApi的act ...

  9. 获取机器安装.NET版本的几种方式

    当调查应用程序问题时,通常需要先确认目标机器所安装的 .NET Framework 的版本.可以通过如下方式来确认版本号: 通过控制面板安装程序查询 通过查询注册表获取版本信息 通过查看安装目录获取版 ...

  10. 使用SQL生成DateTime.Ticks

    在项目中我需要使用到一个随机数(Random Number),该随机数将作为 Hashtable 中的 Key 用于唯一索引数据,所以需要保持单机唯一性. 同时该随机数还需要具备可排序性以便对数据进行 ...