poj 3411 Paid Roads(dfs)
Description
A network of m roads connects N cities (numbered from to N). There may be more than one road connecting one city with another. Some of the roads are paid. There are two ways to pay for travel on a paid road i from city ai to city bi: in advance, in a city ci (which may or may not be the same as ai);
after the travel, in the city bi.
The payment is Pi in the first case and Ri in the second case. Write a program to find a minimal-cost route from the city to the city N.
Input
The first line of the input contains the values of N and m. Each of the following m lines describes one road by specifying the values of ai, bi, ci, Pi, Ri ( ≤ i ≤ m). Adjacent values on the same line are separated by one or more spaces. All values are integers, ≤ m, N ≤ , ≤ Pi , Ri ≤ , Pi ≤ Ri ( ≤ i ≤ m).
Output
The first and only line of the file must contain the minimal possible cost of a trip from the city to the city N. If the trip is not possible for any reason, the line must contain the word ‘impossible’.
Sample Input
Sample Output
Source
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 26
#define inf 1e12
int n,m; struct Node{
int b,c,p,r;
};
vector<Node> node[N];
int ans;
int vis[N];
void dfs(int u,int cost){
vis[u]++;
if(cost>=ans) return;
if(u==n){
if(cost<ans)
ans=cost;
return;
} int size=node[u].size();
for(int i=;i<size;i++){
int v=node[u][i].b;
if(vis[v]<=){
int t=;
if(vis[node[u][i].c] && t>node[u][i].p){
t=node[u][i].p;
}
if(t>node[u][i].r){
t=node[u][i].r;
}
dfs(v,t+cost);
vis[v]--;
}
} }
int main()
{
while(scanf("%d%d",&n,&m)==){
for(int i=;i<N;i++) node[i].clear();
//scanf("%d%d",&n,&m);
for(int i=;i<m;i++){
int a,b,c,p,r;
scanf("%d%d%d%d%d",&a,&b,&c,&p,&r);
Node tmp;
//tmp.a=a;
tmp.b=b;
tmp.c=c;
tmp.p=p;
tmp.r=r;
node[a].push_back(tmp);
}
memset(vis,,sizeof(vis));
ans=;
dfs(,);
if(ans==) printf("impossible\n");
else printf("%d\n",ans);
}
return ;
}
poj 3411 Paid Roads(dfs)的更多相关文章
- POJ 3411 Paid Roads(SPFA || DFS)
题目链接 题意 : 要从1城市到n城市,求最短路是多少,从a城市到达b城市的路程,如果你到过c城市,则需要走p,否则走r长. 思路 : 因为可以来回走,所以不能用单纯的最短路,可以用二维SPFA,状态 ...
- 多次访问节点的DFS POJ 3411 Paid Roads
POJ 3411 Paid Roads Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6553 Accepted: 24 ...
- POJ 1251 Jungle Roads (prim)
D - Jungle Roads Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Su ...
- HDU 1815, POJ 2749 Building roads(2-sat)
HDU 1815, POJ 2749 Building roads pid=1815" target="_blank" style="">题目链 ...
- POJ 3009-Curling 2.0(DFS)
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12158 Accepted: 5125 Desc ...
- 题解报告:poj 1321 棋盘问题(dfs)
Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子 ...
- POJ 2251 Dungeon Master(dfs)
Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...
- poj 3411 Paid Roads很水的DFS
题意:给你N 城市和M条道路,每条道路要付的钱,但是如果你在这个道路上你可以付其他道路的钱(跟走到的时候去的话不一样),问你从1走到N最少话费是多少. 直接DFS搜. 链接http://poj.org ...
- POJ 3411 Paid Roads(DFS)
题目链接 点和边 都很少,确定一个界限,爆搜即可.判断点到达注意一下,如果之前已经到了,就不用回溯了,如果之前没到过,要回溯. #include <cstring> #include &l ...
随机推荐
- mysql 基础技术
一.树状结构 参考http://www.cnblogs.com/kingteach/archive/2011/07/05/2098046.html )) begin declare lev int; ...
- Java遍历Map对象的四种方法
在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都实现了Map接口,以下方法适用于任何map实现(HashMap, TreeMap, LinkedHa ...
- Linux下使用JNI的常见问题及解决方案
JNI是java和C/C++混合编程的接口,可以很方便地实现java调用C/C++语言.具体的使用方法,网上有很多教程,在此不做过多介绍.本博客只关注在使用JNI的过程中的常见问题. 1. 生 ...
- android面试题之四
十六.Android中Dalvik和JVM的区别是什么? 1. Dalvik基于寄存器,而JVM基于栈.基于寄存器的虚拟机对于更大的程序来说,在它们编译的时候,花费的时间更短. 2. Dalvik负责 ...
- 设计模式之八:外观模式(Facade)
外观模式: 为子系统中的一系列接口提供了一个统一的界面.外观模式定义了一个高层次的接口以使子系统更加easy使用. Provide a unified interface to a set of in ...
- Linux经常使用命令大全
系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本号 dmidecode -q 显示硬件系统部件 - (SM ...
- JavaScript ----------- 组合继承
继承 实现继承:继承实际的方法.ECMAScript 只支持实现继承,而且其实现基础主要是依靠原型链来实现的. 基本思想是:利用原型来实现一个引用类型继承另外一个引用类型的属性和方法. 原型 - 构造 ...
- 1207.1——C语言 函数
函数可以分为无参函数和有参函数. 无参函数的定义 无参函数定义的一般形式如下:返回值类型 函数名(){ 函数体} 说明: 返回值类型可以是C语言中的任意数据类型. 函数名是标识符的一种,命名规 ...
- struts1:(Struts)ActionForm类及表单数据验证
在Struts的中央控制器中写了Struts的控制器角色,在这篇介绍下Struts的视图!Struts的视图组件:Struts框架中的视图组件主要包括:JSP页面.ActionForm类.Struts ...
- C++服务器设计(六):设备连接的生命周期管理
生命周期介绍 每一个服务器系统的新连接从建立开始时,均会经历多个阶段.比如连接的建立,登录的验证,退出前的资源释放等.同时在具体的消息处理中,还会遇到不可识别的消息事件,或者消息处理时出现数据错误等. ...