当初第一次做的是FPLICE这个题,当时就觉得要用图论去搜索,但是当时陷入死思维就是 dp[][]两个维度都是点,这样就违背了题目的本意,题目给定了一个时间T,在不超过时间T的情况下求最小的消耗,这不就是背包嘛。。。即拿T做容量,在图上面 设置 dp[i][j]表示i点的时候 j时间的最小消耗。

这样走一遍spfa就可以了。也有人把这个叫做 分层图最短路。。。好高端的样子啊

FPOLICE

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <queue>
  5. #define N 110
  6. #define INF 1<<30
  7. using namespace std;
  8. int dp[N][];
  9. int mat[N][N],risk[N][N];
  10. int inq[N][];
  11. int n,T;
  12. struct node
  13. {
  14. int i,t;
  15. };
  16. void bfs()
  17. {
  18. queue <node> q;
  19. q.push((node){,});
  20. memset(inq,,sizeof inq);
  21. dp[][]=;
  22. while (!q.empty())
  23. {
  24. node cur=q.front();
  25. inq[cur.i][cur.t]=;
  26. q.pop();
  27. for (int i=;i<n;i++)if (cur.i!=i){
  28. if (cur.t+mat[cur.i][i]>T) continue;
  29. int nt=cur.t+mat[cur.i][i];
  30. if (dp[i][nt]>dp[cur.i][cur.t]+risk[cur.i][i]){
  31. dp[i][nt]=dp[cur.i][cur.t]+risk[cur.i][i];
  32. if (!inq[i][nt]){
  33. q.push((node){i,nt});
  34. inq[i][nt]=;
  35. }
  36. }
  37. }
  38. }
  39. int ansT=-,ansR=INF;
  40. for (int i=;i<=T;i++){
  41. if (dp[n-][i]<ansR){
  42. ansR=dp[n-][i];
  43. ansT=i;
  44. }
  45. }
  46. if (ansT<) puts("-1");
  47. else printf("%d %d\n",ansR,ansT);
  48. }
  49. int main()
  50. {
  51. int t;
  52. scanf("%d",&t);
  53. while (t--)
  54. {
  55. scanf("%d%d",&n,&T);
  56. for (int i=;i<n;i++){
  57. for (int j=;j<n;j++){
  58. scanf("%d",&mat[i][j]);
  59. }
  60. for (int j=;j<T+;j++)
  61. dp[i][j]=INF;
  62. }
  63. for (int i=;i<n;i++)
  64. for (int j=;j<n;j++){
  65. scanf("%d",&risk[i][j]);
  66. }
  67. bfs();
  68. }
  69. return ;
  70. }

FISHER

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <queue>
  5. #define INF 1<<30
  6. using namespace std;
  7. struct node{
  8. int x,T;
  9. };
  10. int dp[][];
  11. int maT[][],maF[][];
  12. int n,t;
  13. int inq[][];
  14. void spfa()
  15. {
  16. memset(inq,,sizeof inq);
  17. queue <node> q;
  18. q.push((node){,});
  19. dp[][]=;
  20. while (!q.empty())
  21. {
  22. node u=q.front();q.pop();
  23. inq[u.x][u.T]=;
  24. for (int v=;v<n;v++){
  25. if (v==u.x) continue;
  26. int tot=u.T+maT[u.x][v];
  27. if (tot>t) continue;
  28. if (dp[v][tot]>dp[u.x][u.T]+maF[u.x][v]){
  29. dp[v][tot]=dp[u.x][u.T]+maF[u.x][v];
  30. if (!inq[v][tot]){
  31. q.push((node){v,tot});
  32. inq[v][tot]=;
  33. }
  34. }
  35. }
  36. }
  37. }
  38. int main()
  39. {
  40. while (scanf("%d%d",&n,&t) && n)
  41. {
  42. for (int i=;i<n;i++)
  43. for (int j=;j<n;j++) scanf("%d",&maT[i][j]);
  44. for (int i=;i<n;i++)
  45. for (int j=;j<n;j++) scanf("%d",&maF[i][j]);
  46. for (int i=;i<=n;i++){
  47. for (int j=;j<=t+;j++) dp[i][j]=INF;
  48. }
  49. spfa();
  50. int ans=INF,loc=;
  51. for (int i=;i<=t;i++){
  52. if (ans>dp[n-][i]){
  53. ans=dp[n-][i];
  54. loc=i;
  55. }
  56. }
  57. printf("%d %d\n",ans,loc);
  58. }
  59. return ;
  60. }

SPOJ FISHER + FPOLICE SPFA+背包的更多相关文章

  1. In Action(SPFA+01背包)

    In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  2. 【8.31校内测试】【找规律二分】【DP】【背包+spfa】

    打表出奇迹!表打出来发现了神奇的规律: 1 1 2 2 3 4 4 4 5 6 6 7 8 8 8 8 9 10 10 11 12 12 12 13 14 14 15 16 16 16 16 16.. ...

  3. hdu6007 spfa+完全背包

    题意:给你M,N,K,代表你有M点法力值,N个物品,K个制造方式 接下来N行,如果以1开头则代表既能卖又能合成,0代表只能卖. 然后K行,每行第一个数是要合成的东西,第二个数代表有几对,每对第一个数是 ...

  4. SPOJ 181 - Scuba diver 二维背包

    潜水员要潜水,给出n个气缸(1<=n<=1000),每个气缸中有氧气量为ti,氮气量为ai,气缸重量为wi(1<=ti<=21,1<=ai<=79,1<=wi ...

  5. HDU 6007 Mr. Panda and Crystal (背包+spfa)

    题意:你生活在一个魔法大陆上,你有n 魔力, 这个大陆上有m 种魔法水晶,还有n 种合成水晶的方式,每种水晶价格告诉你,并且告诉你哪些水晶你能直接造出来,哪些你必须合成才能造出来,问你n魔力最多能卖多 ...

  6. SPOJ RENT 01背包的活用+二分

    这个题目给定N航班的发出时间和结束时间以及价值,要求不冲突时间的最大价值 第一时间想到经典的N方DP,即对航班按发出时间排一下序之后每个i对前面的都扫一遍 时间过不了N有10万,只能想优化了,一开始想 ...

  7. SPOJ:Decreasing Number of Visible Box(不错的,背包?贪心?)

    Shadowman loves to collect box but his roommates woogieman and itman don't like box and so shadowman ...

  8. hdu3339 In Action(Dijkstra+01背包)

    /* 题意:有 n 个站点(编号1...n),每一个站点都有一个能量值,为了不让这些能量值连接起来,要用 坦克占领这个站点!已知站点的 之间的距离,每个坦克从0点出发到某一个站点,1 unit dis ...

  9. nyoj 203 三国志(最短路加01背包)

    三国志 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 <三国志>是一款很经典的经营策略类游戏.我们的小白同学是这款游戏的忠实玩家.现在他把游戏简化一下, ...

随机推荐

  1. 全面理解Java中的String数据类型

    1. 首先String不属于8种基本数据类型,String是一个对象. 因为对象的默认值是null,所以String的默认值也是null:但它又是一种特殊的对象,有其它对象没有的一些特性. 2. ne ...

  2. Codeforces 590 A:Median Smoothing

    A. Median Smoothing time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. PAN3501与AS3933完美兼容替代

    现在不少校园门禁卡都是采用奥地利的AS3933,市场需求是供不应求,当然价格上还是不断上升趋势.成本上压力也是越来越大,不少厂家在寻找能替代软硬件兼容AS3933的芯片方案.今天我就为大家介绍一款能否 ...

  4. CentOS 7安装/卸载Redis,配置service服务管理

    Redis简介 Redis功能简介 Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件. 相比于传统的关系型数据库,Redis的存储方式是key-va ...

  5. HTML速写

    1. E 代表HTML标签. 2. E#id 代表id属性. 3. E.class 代表class属性. 4. E[attr=foo] 代表某一个特定属性. 5. E{foo} 代表标签包含的内容是f ...

  6. html基础与入门

    html就是指一个html文件,它是由各种标签组成的 html分为 < !DOCTYPE html > 和 Head 和 Body Head title+meta+link+style B ...

  7. 040、Java中逻辑运算之短路与运算“&&”

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  8. SQLlite的olestr

    关于SQLite的connection string说明:http://www.connectionstrings.com/sqlite/ SQLite GUI客户端列表:http://www.sql ...

  9. delphi http请求用到的编码方式

    uses HttpEncode: HttpEncode(AnsiToUtf8('***'))

  10. springboot - 返回xml error 从自定义的 ErrorController

    1.概览 2.在<springboot - 返回JSON error 从自定义的 ErrorController>基础上,做如下调整: 1).新增Attribute类和Error类 pac ...