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. Handsontable 新增一行 默认值

    效果图:

  2. Redis + Jedis + Spring 实例(对象的操作)

        目录(?)[+] 不得不说,用哈希操作来存对象,有点自讨苦吃! 不过,既然吃了苦,也做个记录,也许以后API升级后,能好用些呢?! 或许,是我的理解不对,没有真正的理解哈希表. 一.预期 接上 ...

  3. Session,有没有必要使用它?

    阅读目录 开始 Session的来龙去脉 Session对并发访问的影响 Session的缺点总结 不使用Session的替代方法 Asp.net MVC 中的Session 现有的代码怎么办? 今天 ...

  4. iOS ARC注释和错误的解决方法在使用

    1.一个错误The current deployment target does not support automated __weak references 这个错误被所述支持iOS版本号不支持相 ...

  5. BOOST 线程完全攻略 - 扩展 - 线程消息通讯

      // controlled_module_ex.hpp : controlled_module类的扩展 // 增强线程之间消息通讯 // 增加线程安全启动和安全关闭功能 // 增加定时器功能 #p ...

  6. Qt导出Excel的简单实现

    QAxObject对COM对象进行了封装,QAxObject派生自QAxBase,而后者提供了一组API通过IUnknown(不清楚IUnknown的同学可以去看看COM对象模型)指针直接访问COM对 ...

  7. poj1562--Oil Deposits

    Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...

  8. 改进的简单Tooltips显示

    使用js简单改进了Tooltips的显示效果,可进一步使用CSS对改进的Tooltips进行美化. 前台布局代码: <asp:Panel ID="Panel1" runat= ...

  9. Android-----输入法的显示和隐藏

    /** * 控制手机虚拟键盘的显示和隐藏 */public class InputMethodUtil { /** * 隐藏虚拟键盘 * @param v  参数v为获取焦点对象view */ pub ...

  10. Django 实战 之 搭项目(正在更新)

    系统:win10 python版本:python 3.5 工具: pyCharm 3.4 professional 源码来源:https://github.com/ouzhigang/django-o ...