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的钱.求的是最少要花 ...
随机推荐
- 【Spring】容器刷新(refresh)流程解析
一.概述 二.prepareRefresh() 三.obtainFreshBeanFactory() 四.prepareBeanFactory(beanFactory); 五.postProcessB ...
- springcloud(五):Spring Cloud 配置中心的基本用法
Spring Cloud 配置中心的基本用法 1. 概述 本文介绍了Spring Cloud的配置中心,介绍配置中心的如何配置服务端及配置参数,也介绍客户端如何和配置中心交互和配置参数说明. 配置中心 ...
- Python数据库小程序
源代码: # dict1 是 字典 , 用来对应相应元素的下标,我们将文件转成列表,对应的也就是文件的下标,通过下标来找文件元素 dict1 = {'sort':0 , 'name':1 ,'age' ...
- 开发必配的Finder设置
1.显示标签页.显示路径栏.显示状态栏的设置位置,在访达->显示-> 显示状态栏 个人三个都设置了,但是觉得显示状态栏用的并不多,反而多一行,下面是显示状态栏的效果,主要可以一眼看出有多少 ...
- Servlet跳转方式sendReDirect()和forward()
在web应用服务中,经常会面对不同SERVLET之间的跳转,目前我们可以通过以下两种方式实现: 1.RequestDispatcher.forward() 2.ServletResponse.send ...
- maven的使用解说
maven周期及项目中的应用: 周期如下: 1.default生命周期,部署项目(jar包的依赖管理) 2.clear生命周期,项目清理工作 3.site生命周期,处理项目中产生的文档信息 应用: 1 ...
- 从网页跳转到自己的app
展开该数据并点击 Item 0.你将在这里定义自定义 URL scheme 的名字.只需要名字,不要在后面追加 :// — 比如,如果你输入 iOSDevApp,你的自定义 url 就是 iOSDev ...
- Knative 实战:三步走!基于 Knative Serverless 技术实现一个短网址服务
短网址顾名思义就是使用比较短的网址代替很长的网址.维基百科上面的解释是这样的: 短网址又称网址缩短.缩短网址.URL 缩短等,指的是一种互联网上的技术与服务,此服务可以提供一个非常短小的 URL 以代 ...
- charles 镜像工具
本文参考:charles 镜像工具 镜像工具会在你浏览指定网站时,把抓取到的文件克隆一份,并保存在你指定的路径下: 注意:如果你配置是www.aaa.com; 那么只会抓这个域名下的文件,这个域名如果 ...
- 从零开始入门 K8s | 应用编排与管理(酒祝)
一.需求来源 背景问题 首先来看一下背景问题.如下图所示:如果我们直接管理集群中所有的 Pod,应用 A.B.C 的 Pod,其实是散乱地分布在集群中. 现在有以下的问题: 首先,如何保证集群内可用 ...