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. 使用QQ互联登录应用

    QQ登录集成插件简介 互联网应用越来越多,通常每一个应用都会要求用户注册登录,粗略估记一下,QQ,微博,微信,银行帐号.邮箱,招聘网站账户,淘宝帐号,支付宝帐号,公司OA帐号....粗略算一下,十几个 ...

  2. VSCode集成Git代码管理

    一.安装和配置VSCode与Git 1.下载Git并安装: https://git-scm.com/download/ 2.下载VSCode并进行安装: https://code.visualstud ...

  3. 【HTML+CSS】(2)CSS Sprite雪碧图

    1. 雪碧图的使用场景 (1). 静态图片.不随用户信息的变化而变化 (2). 小图片.图片容量比較小 (3). 载入量比較大 一些大图不建议拼成雪碧图,比如淘宝站点的导航图片都是使用的雪碧图. 2. ...

  4. FreeSWITCH技巧:如何向通话的另一方号码发送dtmf?

    注:这里的文章都是本人的日常总结,请尊重下个人的劳动成果,转载的童鞋请注明出处,谢谢. 如您转载的文章发生格式错乱等问题而影响阅读,可与本人联系,无偿提供本文的markdown源代码. 联系邮箱:ji ...

  5. ultragrid checkbox

    울트라그리드에 체크박스 넣을 사용하는 속성. cols["checked"].Header.Caption = ""; cols["checked ...

  6. Unity3D刚体不同力的测试(ForceMode,AddForce,RelativeAddForce)

    摘自圣典的一段翻译: ForceAdd a continuous force to the rigidbody, using its mass.添加一个可持续力到刚体,使用它的质量.Accelerat ...

  7. Struts2动作

    ActionContext 每一个请求的处理都在一个独立的线程中.每一个线程都有一个ActionContext对象.它包括了ValueStack和HttpServletRequest的东西. Stru ...

  8. [转]AngularJS ui-router (嵌套路由)

    本文转自:http://www.oschina.net/translate/angularjs-ui-router-nested-routes http://www.codeproject.com/A ...

  9. Oracle与Mysql插入多行数据

    Oracle 一. insert allinto students values ('b10050501','zl')into students values ('b10050502','zjw')s ...

  10. FirstLetterUtil

    package cn.edu.hbcf.common.utils; public class FirstLetterUtil { private static int BEGIN = 45217; p ...