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. POJ 2392 Space Elevator DP

    该题与POJ 1742的思路基本一致:http://www.cnblogs.com/sevenun/p/5442279.html(多重背包) 题意:给你n个电梯,第i个电梯高h[i],数量有c[i]个 ...

  2. 【转】Linux下查看TCP网络连接情况

    查看TCP网络连接情况 命令:netstat -n | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}’ www.2cto.com 返回结 ...

  3. Java配置文件Properties的读取、写入与更新操作

    /** * 实现对Java配置文件Properties的读取.写入与更新操作 */ package test; import java.io.BufferedInputStream; import j ...

  4. 2D和3D空间中计算两点之间的距离

    自己在做游戏的忘记了Unity帮我们提供计算两点之间的距离,在百度搜索了下. 原来有一个公式自己就写了一个方法O(∩_∩)O~,到僵尸到达某一个点之后就向另一个奔跑过去 /// <summary ...

  5. Pure Css 菜单的使用

    本人新手,之前偶尔接触Bootstrap,也做过一些响应式开发,但是都是略显皮毛,公司的业务需求也限制了深入学习. 现着手Pure Css学习,尝试了简单的左边菜单自动隐藏的demo.闲话少说,代码贴 ...

  6. Java的IO以及线程练习

    文件的IO操作: 字节流: 输入字节流:  InputStream 所有输入字节流的基类,抽象类.  FileInputStream 读取文件的输入字节流.  BufferedInputStream ...

  7. 如何实现 iOS 自定义状态栏

    给大家介绍如何实现 iOS 自定义状态栏 Sample Code: 01 UIWindow * statusWindow = [[UIWindow alloc] initWithFrame:[UIAp ...

  8. 推荐一本好书给即将走入工作的程序员and程序媴

    近期买了几本IT届推崇的经典书籍.当中有一本<程序猿修炼之道:专业程序猿必知的33个技巧>.由于这本比較薄,所以先翻着看. 这本书有别于其它的技术书籍,事实上算不上一本技术书籍.它不是教你 ...

  9. java中的二进制

    (1)按位与运算 & 1 & 1 = 1, 0 & 1 = 0 51 & 5  即 0011  0011 & 0000  0101 =0000 0001 = 1 ...

  10. docker四种网络模式

    1,host模式 启动容器时,添加参数--net=host 此模式和宿主机使用的是同1个ip,适合上网. 2,container模式 启动容器时,添加参数--net=container,docker ...