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的钱.求的是最少要花 ...
随机推荐
- Appium+python自动化(三十六)- 士兵突击许三多 - 多个appium服务启动,多个设备启动,多进程并发启动设备-并发测试 - 上(超详解)
简介 前面课程只是启动了单个appium服务,只能控制单台设备.如果需要针对多台设备测试那么该如何处理?而且发现群里的小伙伴们也在时不时地在讨论这个问题,想知道怎么实现的,于是宏哥就决定写一片这样的文 ...
- 分析spring4和spring5日志中的不同
日志在工作中起到关键作用,我们经常使用它来打印关键信息,方便分析,或者是输出错误信息,用于bug排查,spring中同样使用了日志进行信息的输出,但是spring4和spring5之间的日志又有些不同 ...
- fastjson对象,JSON,字符串,map之间的互转
1.对象与字符串之间的互转 将对象转换成为字符串 String str = JSON.toJSONString(infoDo); 字符串转换成为对象 InfoDo infoDo = JSON.pars ...
- DDD领域驱动实践记录
虽然很早之前就已经了解过DDD相关的内容了,但一方面网上理论知识太过碎片化导致难以理解,另一方面实践内容太少导致想动手的时候无从下手.于是就渐渐淡忘了这方面实践的念头. 最近重新了解了DDD相关的知识 ...
- Unity3D_05_理解Unity的新GUI系统(UGUI)
理解Unity的新GUI系统(UGUI) Unity GUI 链接:UnityEngine.UI系统基础类架构图 Unity GUI 链接:UnityEngine Event & Event ...
- golang实现get和post请求的服务端和客户端
服务端 在golang中,实现一个普通的http接口可以处理get请求和x-www-form-urlencoded类型的post请求,而如果想实现处理json数据的post请求,则需要用另外的方式实现 ...
- 15 (OC)* UIGesture
前言 本文主要内容如下: 1. UIGestureRecognizer 属性.方法.代理和七个子类详解. 2. 讲讲 UIGestureRecognizer 和 UITouch 事件的关系. 3. 讲 ...
- springcloud config配置读取优先级
情景描述 最近在修复Eureka的静态页面加载不出的缺陷时,最终发现是远程GIT仓库将静态资源访问方式配置给禁用了(spring.resources.add-mappings=false).虽然最后直 ...
- C语言入门-数据类型
一.C语言的类型 整数:char.short.int.long.longlong 浮点型:float.double.long double 逻辑:bool 指针 自定义类型 类型有何不同 类型名称:i ...
- 洗牌Shuffle'm Up POJ-3087 模拟
题目链接:Shuffle'm Up 题目大意 模拟纸牌的洗牌过程,已知两个牌数相等的牌堆.求解经过多少次洗牌的过程,使牌的顺序与目标顺序相同. 思路 直接模拟,主要是字符串的操作.问题是,如何判断出不 ...