题意:

n个城市,m条双向路径。

从a城市到b城市,有两种付费方法:

1.在c城市预先付费,前提是已经到达了c城市;

2.到了b城市之后,在b付费。

给出路径以及付费信息,求出从1到n的最少花费,或判断从1无法到达n。

思路:

状态压缩dp,每次更新的时候,更新两次,第一次更新未到达的城市,第二次更新可以通过中转城市话费更少到达的城市。

转移方程:

dp[S|(1<<k)][k] = min(dp[S|(1<<k)][k] ,dp[S][j] + cost[j][k]),其中cost[j][k]要么是提前,要么是到付,判断一下条件即可。

代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <vector>
 5 using namespace std;
 6 const int inf = 0x3f3f3f3f;
 7 const int N = 12;
 8 int dp[(1<<N)][N];
 9 struct node
10 {
11     int a,b,c,p,r;
12     node(int aa,int bb,int cc,int pp,int rr)
13     {
14         a = aa;
15         b = bb;
16         c = cc;
17         p = pp;
18         r = rr;
19     }
20     node(){};
21 };
22 vector<node> g[N];
23 int main()
24 {
25     int n,m;
26     while (scanf("%d%d",&n,&m)!=EOF)
27     {
28         memset(dp,inf,sizeof(dp));
29         for (int i = 0;i < n;i++) g[i].clear();
30         for (int i = 0;i < m;i++)
31         {
32             int a,b,c,p,r;
33             scanf("%d%d%d%d%d",&a,&b,&c,&p,&r);
34             a--,b--,c--;
35             g[a].push_back(node(a,b,c,p,r));
36         }
37         dp[1][0] = 0;
38         for (int i = 0;i < (1<<n);i++)
39         {
40             for (int j = 0;j < n;j++)//第一次
41             {
42                 if (!(i&(1<<j))) continue;
43                 for (int k = 0;k < g[j].size();k++)
44                 {
45                     int to = g[j][k].b;
46                     int c = g[j][k].c;
47                     dp[i|(1<<to)][to] = min(dp[i|(1<<to)][to],dp[i][j] + g[j][k].r);
48                     if (i&(1<<c)) dp[i|(1<<to)][to] = min(dp[i|(1<<to)][to],dp[i][j] + g[j][k].p);
49                 }
50             }
51             for (int j = 0;j < n;j++)//第二次
52             {
53                 if (!(i&(1<<j))) continue;
54                 for (int k = 0;k < g[j].size();k++)
55                 {
56                     int to = g[j][k].b;
57                     int c = g[j][k].c;
58                     dp[i|(1<<to)][to] = min(dp[i|(1<<to)][to],dp[i][j] + g[j][k].r);
59                     if (i&(1<<c)) dp[i|(1<<to)][to] = min(dp[i|(1<<to)][to],dp[i][j] + g[j][k].p);
60                 }
61             }
62         }
63         int ans = inf;
64         for (int i = 0;i <(1<<n);i++) ans = min(ans,dp[i][n-1]);
65         if (ans >= inf) puts("impossible");
66         else printf("%d\n",ans);
67     }
68     return 0;

转载于:https://www.cnblogs.com/kickit/p/8866472.html

poj 3411 Paid Roads相关推荐

  1. poj 3411 Paid Roads (dfs)

    题目链接 题意:有N个城市被M条道路连接起来了,每两个城市之间可能存在超过一条路,但是城市之间是单向连接的. 每条路是要花费的.每条路的花费可以选择两种方式:1:假如a城市到达b城市,如果之前经过了c ...

  2. pku 3411 Paid Roads DFS+灵活技巧卡节点访问次数

    http://poj.org/problem?id=3411 题意: 给出 n 个节点 m 条边,求从 1 到 n 的最小花费.有两种支付方式: 1> 预先在城市 Ci (必须先到过该城市)支付 ...

  3. HDU 1815, POJ 2749 Building roads(2-sat)

    HDU 1815, POJ 2749 Building roads 题目链接HDU 题目链接POJ 题意: 有n个牛棚, 还有两个中转站S1和S2, S1和S2用一条路连接起来. 为了使得随意牛棚两个 ...

  4. 【POJ - 1724 】ROADS (带限制的最短路 或 dfs 或 A*算法,双权值)

    题干: N cities named with numbers 1 ... N are connected with one-way roads. Each road has two paramete ...

  5. poj 3411 1724

    题目:http://poj.org/problem?id=3411 题意:n 个城市,m 条路,每条路都有两种情况的费用,一种是 在 b 城市付费 r,一种是提前在 c 城市付费 p 元,问从 1 到 ...

  6. POJ 1251 Jungle Roads

    题意:给你n个点  n-1行每行代表的是这个点到给定点的距离   求最短路 解题思路:开始是用getchar  发现runtime error   后来用了  字符串  才改进了   裸Kruskal ...

  7. 【POJ - 2631】Roads in the North (树的直径,模板)

    题干: Building and maintaining roads among communities in the far North is an expensive business. With ...

  8. 【POJ - 2631 】Roads in the North(树的直径)

    题干: Building and maintaining roads among communities in the far North is an expensive business. With ...

  9. POJ 1947 Rebuilding Roads (树dp + 背包思想)

    题目链接:http://poj.org/problem?id=1947 一共有n个节点,要求减去最少的边,行号剩下p个节点.问你去掉的最少边数. dp[u][j]表示u为子树根,且得到j个节点最少减去 ...

  10. POJ 2749 Building roads 2-sat+二分答案

    把爱恨和最大距离视为限制条件,可以知道,最大距离和限制条件多少具有单调性 所以可以二分最大距离,加边+check 1 #include<cstdio> 2 #include<algo ...

最新文章

  1. 手把手教你,Java如何实现二维码?【附源码】
  2. simply scheme 第一章 练习+ 看书计划
  3. 针对新手的Java EE7和Maven项目–第2部分–为我们的应用程序定义一场简单的战争...
  4. 前端学习(693):for循环案例之求出偶数奇数之和
  5. java 返回第k小的数_java – 给定n和k,返回第k个置换序列
  6. (四)在MLOps管道中进行持续训练
  7. 解决不同浏览器下载excel文件中文名称乱码问题
  8. 浪潮之巅阅读笔记02
  9. 国有数据要素市场的政策红利,你get 到了吗?
  10. IDEA中项目编码格式设置
  11. 数据加密-国密SM2对数据进行加解密
  12. java双层list扁平化,浅谈java8 stream flatMap流的扁平化操作
  13. 16条生活潜规则:没人明说,但很重要
  14. 全球最大同性交友平台骚操作
  15. 完美的Pornhub风格的Logo生成器,在线工具
  16. 使用 GCM 网络管理工具优化电池使用
  17. java nmt_强大的nmt
  18. c语言中sqrt和disc,C ++编程中的Sqrt,sqrtl和sqrtf
  19. C# Settings.settings
  20. 今天搞了个天猫抽奖机器人,祝大家都能中奖!

热门文章

  1. Windows下VB6.0开发——关于String类型数据的思考
  2. C语言:要求输入一个字符,如果这个字符是小写字母,将这个字母转换成大写字母,否则保持不变
  3. 【汇编语言】【ARM扩展资料】数据表示
  4. Spark数据倾斜是如何造成的
  5. nginx之lua_shared_dict命令
  6. 2017年上半年软件设计师试题-04
  7. 合并HTTP请求 vs 并行HTTP请求,到底谁更快?
  8. Netty中有哪些自带的ChannelHandler?
  9. Oracle的数据并发与一致性详解(下)
  10. OwinStartupAttribute