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

Northeastern Europe 2002, Western Subregion
 
直接dfs回溯找出最小的花费即可,以为n很小
 
 #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)的更多相关文章

  1. POJ 3411 Paid Roads(SPFA || DFS)

    题目链接 题意 : 要从1城市到n城市,求最短路是多少,从a城市到达b城市的路程,如果你到过c城市,则需要走p,否则走r长. 思路 : 因为可以来回走,所以不能用单纯的最短路,可以用二维SPFA,状态 ...

  2. 多次访问节点的DFS POJ 3411 Paid Roads

    POJ 3411 Paid Roads Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6553   Accepted: 24 ...

  3. POJ 1251 Jungle Roads (prim)

    D - Jungle Roads Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Su ...

  4. HDU 1815, POJ 2749 Building roads(2-sat)

    HDU 1815, POJ 2749 Building roads pid=1815" target="_blank" style="">题目链 ...

  5. POJ 3009-Curling 2.0(DFS)

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12158   Accepted: 5125 Desc ...

  6. 题解报告:poj 1321 棋盘问题(dfs)

    Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子 ...

  7. 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 ...

  8. poj 3411 Paid Roads很水的DFS

    题意:给你N 城市和M条道路,每条道路要付的钱,但是如果你在这个道路上你可以付其他道路的钱(跟走到的时候去的话不一样),问你从1走到N最少话费是多少. 直接DFS搜. 链接http://poj.org ...

  9. POJ 3411 Paid Roads(DFS)

    题目链接 点和边 都很少,确定一个界限,爆搜即可.判断点到达注意一下,如果之前已经到了,就不用回溯了,如果之前没到过,要回溯. #include <cstring> #include &l ...

随机推荐

  1. 《Java程序员面试笔试宝典》之switch使用时有哪些注意事项

    switch语句用于多分支选择,在使用switch(expr)的时候,expr只能是一个枚举常量(内部也是由整型或字符类型实现)或一个整数表达式,其中整数表达式可以是基本类型int或其对应的包装类In ...

  2. js实现a标签超链接提交form表单的方法

    <a class="regButton"    id="saveRegister" onclick="document.getElementBy ...

  3. C++11里面的Lambda表达式

    Lambda Expressions in C++ C++中的Lambda表达式 In Visual C++, a lambda expression—referred to as a lambda— ...

  4. 男同胞爱小秘籍--作为爱他的女朋友了几天C规划

    各位男同胞,不知道你的女朋友没有在过去的一问天,你这个问题~~ 场景重现: 女友:"今天天气不错." 你们:"对" 女友:"今天是我们知道它的最初几天 ...

  5. 获取对象类型(swift)

    获取对象类型(swift) by 伍雪颖 let date = NSDate() let name = date.dynamicType println(name) let string = &quo ...

  6. [Spring入门学习笔记][Spring Boot]

    什么是Spring Boot Spring Boot正是在这样的一个背景下被抽象出来的开发框架,它本身并不提供Spring框架的核心特性以及扩展功能,只是用于快速.敏捷地开发新一代基于Spring框架 ...

  7. WebApplication和WebSite的区别

    不同点 1. 创建方式不同 一个是FILE->NEW->PROJECT->ASP.NET WEB APPLICATION 另外一个是 FILE->NEW->WEBSITE ...

  8. 伪元素first-letter(首字母变大)

    让首字母变大 <p>Do you like to ride a bicycle?</p> p:first-letter{ font-size: 34px; }

  9. oracle 初探内存结构

    数据库的存储机构 分为 逻辑存储结构 和 物理存储结构 逻辑存储结构: 数据库.表空间.段.区.块         物理存储结构: 数据库.控制文件.数据文件.初始化参数文件.OS块等. 一个区只能在 ...

  10. MySQL 基础 之 语句执行顺序

    FORM: 对FROM的左边的表和右边的表计算笛卡尔积.产生虚表VT1 ON: 对虚表VT1进行ON筛选,只有那些符合<join-condition>的行才会被记录在虚表VT2中. JOI ...