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

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

Sample Output

110

Source

Northeastern Europe 2002, Western Subregion

大致题意:

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

注意:路径是有向的。

#include<iostream>
#include<cstring>
using namespace std;
struct node{
int a,b,c,p,r;
}e[];//每条道路的付费规则
int n,m,mincost,vis[];//城市数//道路数//最小总花费//记录城市的访问次数,每个城市最多经过3次
void dfs(int now,int fee){//now:当前所在城市,fee:当前方案的费用
if(now==n&&mincost>fee){
mincost=fee;return ;
}
for(int i=;i<=m;i++){//枚举道路
if(now==e[i].a&&vis[e[i].b]<=){
vis[e[i].b]++;
if(vis[e[i].c])
dfs(e[i].b,fee+e[i].p);
else
dfs(e[i].b,fee+e[i].r);
vis[e[i].b]--;//回溯
}
}
}
int main(){
while(cin>>n>>m){
memset(vis,,sizeof vis);
vis[]=;//从城市1出发,因此预记录到达1次
mincost=;
for(int i=;i<=m;i++)
cin>>e[i].a>>e[i].b>>e[i].c>>e[i].p>>e[i].r;
dfs(,);
if(mincost==)
cout<<"impossible\n";
else
cout<<mincost<<endl;
}
return ;
}

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. 转: 利用RabbitMQ、MySQL实现超大用户级别的消息在/离线收发

    由于RabbitMQ中只有队列(queue)才能存储信息,所以用RabbitMQ实现超大用户级别(百万计)的消息在/离线收发需要对每一个用户创建一个永久队列. 但是RabbitMQ节点内存有限,经测试 ...

  2. Android简单介绍

    1)Android定义: 2)版本号变迁 3)开发类型 4)Android五大组件 5)Android五大布局

  3. NoSQL(四)

    mongodb介绍 https://www.yiibai.com/mongodb/mongodb_drop_collection.html 1.文档性数据库类似于json对象,分布式 mongodb安 ...

  4. mybatis想要在控制台显示sql语句配置文件

    在src目录下创建一个properties文件 配置内容如下 log4j.rootLogger=debug,stdout,logfile log4j.appender.stdout=org.apach ...

  5. unity, iterate immediate children and iterate all children

    遍历所有直接子节点(immediate children): foreach (Transform child in transform) { // do whatever you want with ...

  6. CentOS下yum常用命令

    1.自动搜索最快镜像插件:yum install yum-fastestmirror 2.更换163的源. 首先:备份/etc/yum.repos.d/CentOS-Base.repomv /etc/ ...

  7. CentOS7:gdb出现没有调试信息:Missing Separate debuginfos

    现在刚刚开始学习用gdb调试程序,结果:在centos下,出现这样的错误: gdb在调试程序时候提示 Missing separate debuginfos, use: debuginfo-insta ...

  8. atitit.nfc 身份证 银行卡 芯片卡 解决方案 attilax总结

    atitit.nfc 身份证 银行卡 芯片卡 解决方案 attilax总结 1. nfc(近距离无线通讯技术)  1 2. 工作模式 1 3. NFC 蓝牙 红外具体对比如下表: 2 4. TypeA ...

  9. Monotone Chain Convex Hull(单调链凸包)

    Monotone Chain Convex Hull(单调链凸包)算法伪代码: //输入:一个在平面上的点集P //点集 P 按 先x后y 的递增排序 //m 表示共a[i=0...m]个点,ans为 ...

  10. Powershell对象条件查询筛选

    在 Windows PowerShell 中,与所需的对象数量相比,通常生成的对象数量以及要传递给管道的对象数量要多得多.可以使用 Format cmdlet 来指定要显示的特定对象的属性,但这并不能 ...