Paid Roads POJ - 3411
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, Pi ≤ Ri (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 E题 Paid Roads(poj 3411) 解题报告
题目链接:http://poj.org/problem?id=3411 题目意思:N个city 由 m 条路连接,对于一条路(假设连接Cityia和 Cityb),如果从Citya 去 Cityb的途 ...
- 多次访问节点的DFS POJ 3411 Paid Roads
POJ 3411 Paid Roads Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6553 Accepted: 24 ...
- 【题解】Paid Roads [SP3953] [Poj3411]
[题解]Paid Roads [SP3953] [Poj3411] 传送门:\(\text{Paid}\) \(\text{Roads}\) \(\text{[SP3953]}\) \(\text{[ ...
- poj 3411 Paid Roads(dfs)
Description A network of m roads connects N cities (numbered to N). There may be more than one road ...
- poj 3411 Paid Roads很水的DFS
题意:给你N 城市和M条道路,每条道路要付的钱,但是如果你在这个道路上你可以付其他道路的钱(跟走到的时候去的话不一样),问你从1走到N最少话费是多少. 直接DFS搜. 链接http://poj.org ...
- POJ 3411 Paid Roads(DFS)
题目链接 点和边 都很少,确定一个界限,爆搜即可.判断点到达注意一下,如果之前已经到了,就不用回溯了,如果之前没到过,要回溯. #include <cstring> #include &l ...
- POJ 3411 Paid Roads(SPFA || DFS)
题目链接 题意 : 要从1城市到n城市,求最短路是多少,从a城市到达b城市的路程,如果你到过c城市,则需要走p,否则走r长. 思路 : 因为可以来回走,所以不能用单纯的最短路,可以用二维SPFA,状态 ...
- poj 3411 Paid Roads
题意:有m条路,n座城市,走这些路是要付费的,每条路由两种付费方案,设一条路两端是a,b,如果走完这条路在b点付费的话,应付r,如果走这条路之前在c点付费的话,应付p,求从1端点走到n端点的最小费用. ...
- POJ 3411 Paid Roads (状态压缩+BFS)
题意:有n座城市和m(1<=n,m<=10)条路.现在要从城市1到城市n.有些路是要收费的,从a城市到b城市,如果之前到过c城市,那么只要付P的钱, 如果没有去过就付R的钱.求的是最少要花 ...
随机推荐
- Python 的整数与 Numpy 的数据溢出
某位 A 同学发了我一张截图,问为何结果中出现了负数? 看了图,我第一感觉就是数据溢出了.数据超出能表示的最大值,就会出现奇奇怪怪的结果. 然后,他继续发了张图,内容是 print(100000*20 ...
- Linux系统下安装zookeeper教程
环境: 1.VMware® Workstation 12 Pro 2.CentOS7 3.zookeeper-3.4.6 安装步骤 1.下载zookeeper 本文使用的zookeeper下载地址如下 ...
- Android源码阅读技巧--查找开发者选项中显示触摸操作源码
在开发者模式下,在开发者选项中,可以勾选“显示触摸操作”,然后只要点击屏幕就会在点击的位置有圈圈显示.如何找到绘制圈圈的代码部分,有什么技巧来阅读代码量这么大的android系统源码呢?以下请跟着小老 ...
- 阿里短信封装SDK TP3.2
1.阿里短信接口需要企业认证: 2.短信需要短信模板 <?php /** * 阿里云短信验证码发送类 * @param string $accessKeyId key * @param stri ...
- 那些年,我们误解的 JavaScript 闭包
说到闭包,大部分的初始者,都是谈虎色变的.最近对闭包,有了自己的理解,就感觉.其实我们误解闭包.也被网上各种说的闭包的解释给搞迷糊. 一句话:要想理解一个东西还是看权威的东西. 下面我来通俗的讲解一个 ...
- FreeSql (十八)导航属性
导航属性是 FreeSql 的特色功能之一,可通过约定配置.或自定义配置对象间的关系. 导航属性有 OneToMany, ManyToOne, ManyToMany, OneToOne, Parent ...
- localStorage详细总结
一.localStorage简介: 在HTML5中,新加入了一个localStorage特性,这个特性主要是用来作为本地存储来使用的,解决了cookie存储空间不足的问题(cookie中每条cooki ...
- mybatis if判断两个值是否相等存在的坑啊
1.使用“==”比较 字符类型 的值 用“==”比较的使用场景: 不管你用的什么类型的变量,只要变量的值是字符类型就用“==” 产生原因: 在mybatis中如果<if>标签用一个“=”判 ...
- Day 15 文件打包与压缩
1.什么是文件压缩? 将多个文件或目录合并成为一个特殊的文件.比如: 搬家...脑补画面 img. 2.为什么要对文件进行压缩? 当我们在传输大量的文件时,通常都会选择将该文件进行压缩,然后在进行传输 ...
- Git同步更新操作GitHub和码云仓库上面的代码
一.前言 问题: 小编在生活中,一般都是将代码保存到github上,但由于国内的码云仓库确实速度比github快很多,用起来也很方便,于是后来就慢慢转码云了,当然小编在github上的代码也不想放弃更 ...