SPOJ FISHER + FPOLICE SPFA+背包
当初第一次做的是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+背包的更多相关文章
- In Action(SPFA+01背包)
In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- 【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.. ...
- hdu6007 spfa+完全背包
题意:给你M,N,K,代表你有M点法力值,N个物品,K个制造方式 接下来N行,如果以1开头则代表既能卖又能合成,0代表只能卖. 然后K行,每行第一个数是要合成的东西,第二个数代表有几对,每对第一个数是 ...
- SPOJ 181 - Scuba diver 二维背包
潜水员要潜水,给出n个气缸(1<=n<=1000),每个气缸中有氧气量为ti,氮气量为ai,气缸重量为wi(1<=ti<=21,1<=ai<=79,1<=wi ...
- HDU 6007 Mr. Panda and Crystal (背包+spfa)
题意:你生活在一个魔法大陆上,你有n 魔力, 这个大陆上有m 种魔法水晶,还有n 种合成水晶的方式,每种水晶价格告诉你,并且告诉你哪些水晶你能直接造出来,哪些你必须合成才能造出来,问你n魔力最多能卖多 ...
- SPOJ RENT 01背包的活用+二分
这个题目给定N航班的发出时间和结束时间以及价值,要求不冲突时间的最大价值 第一时间想到经典的N方DP,即对航班按发出时间排一下序之后每个i对前面的都扫一遍 时间过不了N有10万,只能想优化了,一开始想 ...
- SPOJ:Decreasing Number of Visible Box(不错的,背包?贪心?)
Shadowman loves to collect box but his roommates woogieman and itman don't like box and so shadowman ...
- hdu3339 In Action(Dijkstra+01背包)
/* 题意:有 n 个站点(编号1...n),每一个站点都有一个能量值,为了不让这些能量值连接起来,要用 坦克占领这个站点!已知站点的 之间的距离,每个坦克从0点出发到某一个站点,1 unit dis ...
- nyoj 203 三国志(最短路加01背包)
三国志 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 <三国志>是一款很经典的经营策略类游戏.我们的小白同学是这款游戏的忠实玩家.现在他把游戏简化一下, ...
随机推荐
- 全面理解Java中的String数据类型
1. 首先String不属于8种基本数据类型,String是一个对象. 因为对象的默认值是null,所以String的默认值也是null:但它又是一种特殊的对象,有其它对象没有的一些特性. 2. ne ...
- Codeforces 590 A:Median Smoothing
A. Median Smoothing time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- PAN3501与AS3933完美兼容替代
现在不少校园门禁卡都是采用奥地利的AS3933,市场需求是供不应求,当然价格上还是不断上升趋势.成本上压力也是越来越大,不少厂家在寻找能替代软硬件兼容AS3933的芯片方案.今天我就为大家介绍一款能否 ...
- CentOS 7安装/卸载Redis,配置service服务管理
Redis简介 Redis功能简介 Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件. 相比于传统的关系型数据库,Redis的存储方式是key-va ...
- HTML速写
1. E 代表HTML标签. 2. E#id 代表id属性. 3. E.class 代表class属性. 4. E[attr=foo] 代表某一个特定属性. 5. E{foo} 代表标签包含的内容是f ...
- html基础与入门
html就是指一个html文件,它是由各种标签组成的 html分为 < !DOCTYPE html > 和 Head 和 Body Head title+meta+link+style B ...
- 040、Java中逻辑运算之短路与运算“&&”
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- SQLlite的olestr
关于SQLite的connection string说明:http://www.connectionstrings.com/sqlite/ SQLite GUI客户端列表:http://www.sql ...
- delphi http请求用到的编码方式
uses HttpEncode: HttpEncode(AnsiToUtf8('***'))
- springboot - 返回xml error 从自定义的 ErrorController
1.概览 2.在<springboot - 返回JSON error 从自定义的 ErrorController>基础上,做如下调整: 1).新增Attribute类和Error类 pac ...