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

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

FPOLICE

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define N 110
#define INF 1<<30
using namespace std;
int dp[N][];
int mat[N][N],risk[N][N];
int inq[N][];
int n,T;
struct node
{
int i,t;
};
void bfs()
{
queue <node> q;
q.push((node){,});
memset(inq,,sizeof inq);
dp[][]=;
while (!q.empty())
{
node cur=q.front();
inq[cur.i][cur.t]=;
q.pop();
for (int i=;i<n;i++)if (cur.i!=i){
if (cur.t+mat[cur.i][i]>T) continue;
int nt=cur.t+mat[cur.i][i];
if (dp[i][nt]>dp[cur.i][cur.t]+risk[cur.i][i]){
dp[i][nt]=dp[cur.i][cur.t]+risk[cur.i][i];
if (!inq[i][nt]){
q.push((node){i,nt});
inq[i][nt]=;
}
}
}
}
int ansT=-,ansR=INF;
for (int i=;i<=T;i++){
if (dp[n-][i]<ansR){
ansR=dp[n-][i];
ansT=i;
}
}
if (ansT<) puts("-1");
else printf("%d %d\n",ansR,ansT);
}
int main()
{
int t;
scanf("%d",&t);
while (t--)
{
scanf("%d%d",&n,&T);
for (int i=;i<n;i++){
for (int j=;j<n;j++){
scanf("%d",&mat[i][j]);
}
for (int j=;j<T+;j++)
dp[i][j]=INF;
}
for (int i=;i<n;i++)
for (int j=;j<n;j++){
scanf("%d",&risk[i][j]);
}
bfs();
}
return ;
}

FISHER

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define INF 1<<30
using namespace std;
struct node{
int x,T;
};
int dp[][];
int maT[][],maF[][];
int n,t;
int inq[][];
void spfa()
{
memset(inq,,sizeof inq);
queue <node> q;
q.push((node){,});
dp[][]=;
while (!q.empty())
{
node u=q.front();q.pop();
inq[u.x][u.T]=;
for (int v=;v<n;v++){
if (v==u.x) continue;
int tot=u.T+maT[u.x][v];
if (tot>t) continue;
if (dp[v][tot]>dp[u.x][u.T]+maF[u.x][v]){
dp[v][tot]=dp[u.x][u.T]+maF[u.x][v];
if (!inq[v][tot]){
q.push((node){v,tot});
inq[v][tot]=;
}
}
}
}
}
int main()
{
while (scanf("%d%d",&n,&t) && n)
{
for (int i=;i<n;i++)
for (int j=;j<n;j++) scanf("%d",&maT[i][j]);
for (int i=;i<n;i++)
for (int j=;j<n;j++) scanf("%d",&maF[i][j]);
for (int i=;i<=n;i++){
for (int j=;j<=t+;j++) dp[i][j]=INF;
}
spfa();
int ans=INF,loc=;
for (int i=;i<=t;i++){
if (ans>dp[n-][i]){
ans=dp[n-][i];
loc=i;
}
}
printf("%d %d\n",ans,loc);
}
return ;
}

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. 2017 青岛网络赛 Chenchen, Tangtang and ZengZeng

    Chenchen, Tangtang and ZengZeng are starting a game of tic-tac-toe, played on a 3 × 3 board. Initial ...

  2. Ubuntu 16.04.4下安装apache服务

     Ubuntu 16.04.4下安装apache服务: 一.首先,准备需要的预装环境 需要c++,make,gcc,apr  apr-util  pcre.(如果后面报错缺少什么组件,可以百度搜方法. ...

  3. MySQL设置各类字符集

    一.查看字符集编码: 登录mysql show variables like '%character%'; 二.修改编码: 编辑/etc/my.cnf ,设置后的配置文件如下: [root@node0 ...

  4. node - multer 加图片后缀

    var multer = require('multer') var storage = multer.diskStorage({   destination: function (req, file ...

  5. 微信小程序—页面跳转

    问题: 实现页面跳转:index页面通过按钮跳转到next页面 方法: 1.页面index.wxml增加一个按钮 // index.wxml <button bindtap="jump ...

  6. Day6 - M - 动态逆序对 HYSBZ - 3295

    对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删 除m个元素,你的任务是在每次删除一个元素之前统计整个序列的逆序对数 I ...

  7. 软件包管理:RPM包管理-yum在线管理

    CentOS 是免费的的 RedHat需要付费 1.IP地址配置 setup  #使用setup工具 (这种方式配置的永久有效 同时还可以配置掩码 网关等) 直接输入setup就会弹出(注意的是该命令 ...

  8. springcloud--ruul(路由网关)

    Zuul的主要功能就是路由转发和过滤器 实例: 1:添加依赖pom.xml: <dependencies> <dependency> <groupId>org.sp ...

  9. springboot启动微服务项目时,启动后没有端口号信息,也访问不了

    2018-06-05 13:43:42.282 [localhost-startStop-1] DEBUG org.apache.catalina.core.ContainerBase - Add c ...

  10. C++面试常见问题——02动态分配内存

    动态分配内存 C++动态内存 C++程序中内存分为两个部分 堆:程序中未使用的内存,在程序运行时可用于动态分配内存. 栈:函数内部申明的所有变量都将占用栈内存. 很多时候不知道一个程序到底需要多少内存 ...