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. java基础讲解04-----数据类型和运算符

    1.java的基本数据类型 1.数值型  { 整数型   byte  , short  ,int  ,long 浮点型   float , double } 2.字符型 3.布尔型 2.运算符 1.赋 ...

  2. server2008服务器IIS7 +PHP5.3出现500错误的排错方法

    Windows7 IIS 500 – 内部服务器错误解决方案 1.解决方法:打开IIS,在全局功能视图中找到“错误页”,双击进去后,看最右边的“操作”下的“编辑功能设置…”,将“错误响应”下的“详细错 ...

  3. Centos下运行定时任务Crontab命令介绍

    1.Cron的启动与关闭 因为Cron是Linux的内置服务.能够用下面的方法启动.关闭这个服务: /sbin/service crond start           //启动服务 /sbin/s ...

  4. Atitit.检测文本文件的编码 自动获取文件的中文编码

    Atitit.检测文本文件的编码 自动获取文件的中文编码 1 不能使用load来检测编码..1 2 使用convert来检测编码1 3 程序检测文本编码2 3.1 根据utf bom头2 3.2 检测 ...

  5. 413. Reverse Integer【easy】

    Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...

  6. mongodb数据库shell

    mongoexport -d mofangdb -c log_user_access_index --type=csv -f _id,uid,page,date -o log_user_access_ ...

  7. linux学习笔记11---命令more

    more命令,功能类似 cat ,cat命令是整个文件的内容从上到下显示在屏幕上. more会以一页一页的显示方便使用者逐页阅读,而最基本的指令就是按空白键(space)就往下一页显示,按 b 键就会 ...

  8. uinty实现玩家尾随鼠标位置平滑旋转角度

    首先我们要在场景中加入一个quad平面作为地板, 然后指定Layer为Floor,然后移除mesh renderer组件 然后加入脚本 脚本主要思想是从屏幕中心投出一条射线到地板, 然后获取相应坐标, ...

  9. 如何在调试Window App时,触发 Suspending ,Resuming 等事件

    Visual Studio prevents Windows from suspending an app that is attached to the debugger. This is to a ...

  10. Tuning 13 Using oracle blocks Efficiently

    推进使用自动管理 automatic segment 1 个 Blocks = 2的幂次方倍 tablespace 像一块地 segment 像一个房子 extents 向一个装砖头的框 blocks ...