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. Shell下通过echo+telnet在远端执行命令

    创建脚本cmd.sh,用于输入telnet的用户与密码,以及生成远端需要执行的命令   执行命令 MY_SIGN=/tmp/sign; (sh cmd.sh ) | (telnet localhost ...

  2. Linux各种包安装及命令

    1.Locate yum -y install mlocate 若出现问题: locate: can not stat () `/var/lib/mlocate/mlocate.db': 没有那个文件 ...

  3. qt model/view 架构基础介绍之QListWidget

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' from PyQt4.QtGui import  * from Py ...

  4. Handsontable对单元格的操作

    1.自动填充单元格数据 fillHandle:true/false    //当值为true时,允许拖动单元格右下角,将其值自动填充到选中的单元格 2.合并单元格 mergeCells:[{row:起 ...

  5. 在Ubuntu上为Android系统编写Linux内核驱动程序

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6568411 在智能手机时代,每个品牌的手机都有 ...

  6. xml入门简介--两天学会xml

    前言 在很久以前,笔者曾见到过1000+页的xml书,里面还有n多的概念,XSL,Xquery,让人头痛.无奈最近需要用到,所以在w3c恶补了一下.以下大致整理了一下相关概念,但是对XSL等派生语言没 ...

  7. Web系统如何做到读取客户电脑MAC等硬件信息且兼容非IE浏览器

    我们在实际Web应用中,可能会遇到“需要限定特定的电脑或用户才能使用系统”的问题. 对于一般情况来说,我们用得最多的可能是使用ActiveX控件的方法来实现,但此方案只适用于IE浏览器.为了能兼容不同 ...

  8. 关于iOS9之后的loadViewIfNeeded

    iOS9之前 有些时候因为需要手动调用loadview 但是有风险,系统不再调用viewDidLoad 所以手动调用loadview是错误的 iOS9之后出现了loadViewIfNeeded解决了这 ...

  9. 【转载】NSURLSession教程

    原文:http://www.raywenderlich.com/51127/nsurlsession-tutorial 查理·富尔顿 2013年10月9日, 推特 注意从雷 :这是一个缩写版的一章 i ...

  10. 【转载】"library not found for - "解决办法

    [摘要:正在我们编译的时间偶然候会报那个毛病"library not found for - " 因为是我们正在项目中应用了一些第三圆的库,便比方我再应用百度的静态库文件的时间,报 ...