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 ai, bi, ci, Pi, Ri (1 ≤ i 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, PiRi (1 ≤ i 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

题解:
  一开始看错题了,wa了好多次,真是浪费时间。
  这个题目,状态十分显然,dp[i][s]表示处于节点i,当前经过点的集合为s的最小花费,那么转移就是枚举边转移就可以了,注意,如果可以用情况1就必须用情况1.
  初始化的时候都为inf,让他们不能更新其他状态,然后这个有后效性,必须spfa。
代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
#include <queue>
#define MANXN 15
#define MAXNM 2100
#define ll long long
using namespace std;
int dp[MANXN][<<MANXN],have[MANXN][<<MANXN];
int n,m,num=,inf;
struct edge{
int first;
int next;
int to,c,p,r;
}a[MAXNM*];
struct heapnode{
int id;ll S;
};
queue<heapnode> q; void addedge(int from,int to,int c,int p,int r){
a[++num].to=to,a[num].p=p,a[num].r=r,a[num].c=c;
a[num].next=a[from].first;
a[from].first=num;
} void spfa(){
memset(dp,/,sizeof(dp));inf=dp[][];
memset(have,,sizeof(have));
dp[][]=;q.push((heapnode){,});have[][]=;
while(!q.empty()){
int now=q.front().id;
ll s=q.front().S;
have[now][s]=;
q.pop();
for(int i=a[now].first;i;i=a[i].next){
int to=a[i].to;
if(s&(<<(a[i].c-))){
if(dp[to][s|(<<(to-))]>dp[now][s]+a[i].p){
dp[to][s|(<<(to-))]=dp[now][s]+a[i].p;
if(!have[to][s|(<<(to-))]) {
have[to][s|(<<(to-))]=;
q.push((heapnode){to,s|(<<(to-))});
}
}
}
else{
if(dp[to][s|(<<(to-))]>dp[now][s]+a[i].r){
dp[to][s|(<<(to-))]=dp[now][s]+a[i].r;
if(!have[to][s|(<<(to-))]){
have[to][s|(<<(to-))]=;
q.push((heapnode){to,s|(<<(to-))});
}
}
}
}
}
} int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++){
int x,y,c,p,r;scanf("%d%d%d%d%d",&x,&y,&c,&p,&r);
addedge(x,y,c,p,r);
}
spfa();
int ans=inf;
for(int i=;i<=(<<n)-;i++){
ans=min(ans,dp[n][i]);
}
if(ans==inf)cout<<"impossible";
else cout<<ans;
return ;
}

Paid Roads POJ - 3411的更多相关文章

  1. 广大暑假训练1 E题 Paid Roads(poj 3411) 解题报告

    题目链接:http://poj.org/problem?id=3411 题目意思:N个city 由 m 条路连接,对于一条路(假设连接Cityia和 Cityb),如果从Citya 去 Cityb的途 ...

  2. 多次访问节点的DFS POJ 3411 Paid Roads

    POJ 3411 Paid Roads Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6553   Accepted: 24 ...

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

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

  4. poj 3411 Paid Roads(dfs)

    Description A network of m roads connects N cities (numbered to N). There may be more than one road ...

  5. poj 3411 Paid Roads很水的DFS

    题意:给你N 城市和M条道路,每条道路要付的钱,但是如果你在这个道路上你可以付其他道路的钱(跟走到的时候去的话不一样),问你从1走到N最少话费是多少. 直接DFS搜. 链接http://poj.org ...

  6. POJ 3411 Paid Roads(DFS)

    题目链接 点和边 都很少,确定一个界限,爆搜即可.判断点到达注意一下,如果之前已经到了,就不用回溯了,如果之前没到过,要回溯. #include <cstring> #include &l ...

  7. POJ 3411 Paid Roads(SPFA || DFS)

    题目链接 题意 : 要从1城市到n城市,求最短路是多少,从a城市到达b城市的路程,如果你到过c城市,则需要走p,否则走r长. 思路 : 因为可以来回走,所以不能用单纯的最短路,可以用二维SPFA,状态 ...

  8. poj 3411 Paid Roads

    题意:有m条路,n座城市,走这些路是要付费的,每条路由两种付费方案,设一条路两端是a,b,如果走完这条路在b点付费的话,应付r,如果走这条路之前在c点付费的话,应付p,求从1端点走到n端点的最小费用. ...

  9. POJ 3411 Paid Roads (状态压缩+BFS)

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

随机推荐

  1. explain的关键字段的意义

    mysql提供的explain工具可以输出一些有用的信息. 一下是相关的部分返回值的意义. select_type 表示SELECT的类型,常见的取值有: SIMPLE:简单表,不使用表连接或子查询 ...

  2. EditPlus5.0破解激活

    永久激活用户名激活码: 用户名:Vovan注册码:3AG46-JJ48E-CEACC-8E6EW-ECUAW 然后重启软件即可

  3. Winform中通过代码给PanelControl添加子控件并进行定位

    场景 DevExpress的PanelControl常用进行窗体页面的布局. 一般是拖拽一个PannelControl,然后是再拖拽其他控件. 如果是由代码生成控件并控制布局的话,怎样实现. 关注公众 ...

  4. Volatile与synchronize的区别

    仅靠Volatile不能保证线程的安全性(原子性) 1.Volatile轻量级的,只能修饰变量.synchronize重量级的,还可以修饰方法 2.Volatile只保证数据的可见性,不能用来同步,因 ...

  5. JVM垃圾回收?看这一篇就够了!

    深入理解JVM垃圾回收机制 1.垃圾回收需要解决的问题及解决的办法总览 1.如何判定对象为垃圾对象 引用计数法 可达性分析法 2.如何回收 回收策略 标记-清除算法 复制算法 标记-整理算法 分带收集 ...

  6. Java 教程(开发环境配置+基础语法)

    Java 开发环境配置 在本章节中我们将为大家介绍如何搭建Java开发环境. window系统安装java 下载JDK 首先我们需要下载java开发工具包JDK,下载地址:http://www.ora ...

  7. 疑难杂症----windows7

    这两天换了台win7的机器,每次开机发现时间日期没法更新,第一次手动更新后过了一天以后又恢复成20xx/01/01,头疼ing,网上找了好多办法,在这里记录一下,避免以后再碰到同样的问题. 出现这个问 ...

  8. sql server之SQL SELECT INTO 语句

    SELECT INTO 语句可用于创建表的备份复件. SELECT INTO 语句 SELECT INTO 语句从一个表中选取数据,然后把数据插入另一个表中. SELECT INTO 语句常用于创建表 ...

  9. 【面试题】Java基础部分面试题

    Java基础面试题 Equals与==的区别 使用==比较原生类型如:boolean,,int,char等等,  使用equals()比较对象. 1.  ==是判断两个变量或类型是不是指向同一个内存空 ...

  10. 在vue项目中使用less

    1.安装 less 和 less-loader. 命令:         npm install less less-loader --save-dev   2.打开 build/webpack.ba ...