Paid Roads
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6549   Accepted: 2427

Description

A network of m roads connects N cities (numbered from 1 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 1 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 aibiciPiRi (1 ≤ ≤ m). Adjacent values on the same line are separated by one or more spaces. All values are integers, 1 ≤ m, N ≤ 10, 0 ≤ Pi , Ri ≤ 100, Pi ≤ Ri (1 ≤ ≤ m).

Output

The first and only line of the file must contain the minimal possible cost of a trip from the city 1 to the city N. If the trip is not possible for any reason, the line must contain the word ‘impossible’.

Sample Input

  1. 4 5
  2. 1 2 1 10 10
  3. 2 3 1 30 50
  4. 3 4 3 80 80
  5. 2 1 2 10 10
  6. 1 3 2 10 50

Sample Output

  1. 110

Source

Northeastern Europe 2002, Western Subregion

大致题意:

有n座城市和m(1<=n,m<=10)条路。现在要从城市1到城市n。有些路是要收费的,从a城市到b城市,如果之前到过c城市,那么只要付P的钱,如果没有去过就付R的钱。求的是最少要花多少钱。

注意:路径是有向的。

  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4. struct node{
  5. int a,b,c,p,r;
  6. }e[];//每条道路的付费规则
  7. int n,m,mincost,vis[];//城市数//道路数//最小总花费//记录城市的访问次数,每个城市最多经过3次
  8. void dfs(int now,int fee){//now:当前所在城市,fee:当前方案的费用
  9. if(now==n&&mincost>fee){
  10. mincost=fee;return ;
  11. }
  12. for(int i=;i<=m;i++){//枚举道路
  13. if(now==e[i].a&&vis[e[i].b]<=){
  14. vis[e[i].b]++;
  15. if(vis[e[i].c])
  16. dfs(e[i].b,fee+e[i].p);
  17. else
  18. dfs(e[i].b,fee+e[i].r);
  19. vis[e[i].b]--;//回溯
  20. }
  21. }
  22. }
  23. int main(){
  24. while(cin>>n>>m){
  25. memset(vis,,sizeof vis);
  26. vis[]=;//从城市1出发,因此预记录到达1次
  27. mincost=;
  28. for(int i=;i<=m;i++)
  29. cin>>e[i].a>>e[i].b>>e[i].c>>e[i].p>>e[i].r;
  30. dfs(,);
  31. if(mincost==)
  32. cout<<"impossible\n";
  33. else
  34. cout<<mincost<<endl;
  35. }
  36. return ;
  37. }

poj3411的更多相关文章

  1. 【题解】Paid Roads [SP3953] [Poj3411]

    [题解]Paid Roads [SP3953] [Poj3411] 传送门:\(\text{Paid}\) \(\text{Roads}\) \(\text{[SP3953]}\) \(\text{[ ...

  2. poj3411 Paid Roads

    思路: 搜索.注意点和边都有可能经过多次. 实现: #include <iostream> #include <cstdio> #include <vector> ...

  3. poj分类 很好很有层次感。

    初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      ( ...

  4. 【转】POJ题目分类推荐 (很好很有层次感)

    OJ上的一些水题(可用来练手和增加自信) (poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094)初期: 一. ...

  5. 【转】ACM训练计划

    [转] POJ推荐50题以及ACM训练方案 -- : 转载自 wade_wang 最终编辑 000lzl POJ 推荐50题 第一类 动态规划(至少6题, 和 必做) 和 (可贪心) (稍难) 第二类 ...

  6. POJ 题目分类(转载)

    Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...

  7. (转)POJ题目分类

    初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推. ...

  8. acm常见算法及例题

    转自:http://blog.csdn.net/hengjie2009/article/details/7540135 acm常见算法及例题  初期:一.基本算法:     (1)枚举. (poj17 ...

  9. poj分类

    初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      ( ...

随机推荐

  1. JBOSS整套开发组件整合和配置方法

    http://blog.csdn.net/laigood/article/details/5743712主要是集成jboss,jboss esb,jboss portal,jboss seam,jbo ...

  2. Linux操作系统--help、man和info工具的区别介绍

    http://wenda.tianya.cn/wenda/thread?tid=1d4b0f172f958833Linux操作系统--help.man和info工具的区别介绍 Linux操作系统为我们 ...

  3. 利用docker创建支持centos的ssh镜像

    创建docker镜像需要基础镜像,目前官方已提供了多种基础镜像,参见: https://hub.docker.com/explore/ 要想创建支持centos的ssh镜像,就需要以centos镜像为 ...

  4. C#指南,重温基础,展望远方!(1)C#语言介绍

    1.C#(读作“See Sharp”)是一种简单易用的新式编程语言,不仅面向对象,还类型安全. C# 源于 C 语言系列,C.C++.Java 和 JavaScript 程序员很快就可以上手使用. 2 ...

  5. 怎样防止应用因获取IDFA被AppStore拒绝

    由于Appstore禁止不使用广告而採集IDFA的app上架,友盟提供IDFA版和不含IDFA版两个SDK,两个SDK在数据上并没有差异.採集IDFA是为了防止今后由于苹果可能禁止眼下使用的openu ...

  6. 现在的C语言编辑器里的int范围为什么是-2147483648~2147483647 2014-08-05 10:21 100人阅读 评论(0) 收藏

    下面是引用百度文库的一段话: "这得从二进制的原码说起: 如果以最高位为符号位,二进制原码最大为0111111111111111=215-1=32767 最小为111111111111111 ...

  7. struts和spring整合

    开发流程: 1)引jar包,可以在配置工程中设置用户libarary,然后直接引入.如果在web-inf/lib没有用户导入的lib文件,可以参考问题0的解决方案 需要的是struts_core,sp ...

  8. mysql-multi source replication 配置

    1.关键步骤 change master to master_host='172.16.192.201', master_port, master_user='repl', master_passwo ...

  9. 哪个线程执行 CompletableFuture’s tasks 和 callbacks?

    CompletableFuture尽管在2014年的三月随着Java8被提出来,但它现在仍然是一种相对较新潮的概念.但也许这个类不为人所熟知是好事,因为它很容易被滥用,特别是涉及到使用线程和线程池的时 ...

  10. JAVA连接各种数据库详解

    Java数据库连接(JDBC)由一组用 Java 编程语言编写的类和接口组成.JDBC 为工具/数据库开发人员提供了一个标准的 API,使他们能够用纯Java API 来编写数据库应用程序.然而各个开 ...