timus 1210 Kind Spirits(最短路)(动态规划)
Kind Spirits
Memory limit: 64 MB
to live there. An awful climate, 80 hours working week, ugly girls… He, as well as every
inhabitant of his planet, dreams to get to a planet of N-th level. To the paradise.
hyperspace transfers to some of the (i+1)-st level planets (but
there are no
reverse ways). Every transfer is guarded by a spirit. The spirits are
usually evil: they demand many galactic bank-notes for each transfer.
You know, everyone wants to go to a higher level planet. And one has to
pay for the pleasure. More than Ivanushka can even imagine. However,
extraordinary situations like a lack of a labor-force at one of the
higher level planets sometimes happen, and then the spirits - the guards
of the transfers — become kind. Sometimes they give galactic bank-notes
themselves if only someone goes to their planets.
order to embody his dream of heavenly planet
Ivanushka has done two things. First of all,
he has borrowed a complete map of the Universe. It's written on the map
how much the spirits demand or give for a transfer from this or that
planet to another one of the next higher level. Secondly, he has hired a
staff of young talanted
programmers in order that they will help him to draw the way on the map
from
his planet to the one of Nth level so that he would spend for the
spirits as little money or even earn as much as it is possible.
Input
(0 < N < 30) — an amount of levels of the planets on Ivanushka's
map. Then follow N blocks of information that describe interlevel
transfers. More precisely, the ith informative block describes the
scheme of transfers from (i−1)-st level planets to the ones of
ith level. Those blocks are separated with a line that contains the
only symbol "*". Planets of each level are numbered with sequential positive
integers starting from 1. Each level contains not more than 30 planets. There
is the only planet of 0-level: the one that Ivanushka lives at. The first
line of a block contains a number Ki — an amount of planets
of the ith level. THen follow Ki lines — one for each
planet of the ith level. Every line consists of numbers of planets
separated with a space of the previous (i−1)st level that one can get
from them to the current planet, and the corresponding fees. A fee for each
transfer is an integer number from −32768 to 32767; a negative fee means that
the kind spirit is ready to pay for such a transfer. Each description line
is ended by zero.
Output
might pay for a transfer to some planet of the Nth level. The answer
may be negative: it means that Ivanushka will not only get to a heavenly
planet, but will earn some galactic bank-notes. It's known that there exists
if only one way from Ivanushka's planet to the one of Nth level.
Sample
input | output |
---|---|
3 |
-1 |
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 0x3f3f3f3f
#define mod 10000
typedef long long ll;
using namespace std;
const int N=;
const int M=;
int power(int a,int b,int c){int ans=;while(b){if(b%==){ans=(ans*a)%c;b--;}b/=;a=a*a%c;}return ans;}
int w[N][N],vis[N];
int n,m,k,c,s=;
int sum[N],dp[N];
int main()
{
memset(w,inf,sizeof(w));
memset(dp,inf,sizeof(dp));
sum[]=;
char ch[];
scanf("%d",&n);
for(int i=;i<=n+;i++){
scanf("%d",&m);
sum[i]=sum[i-]+m;
for(int j=;j<=m;j++){
while(~scanf("%d",&k)&&k){
scanf("%d",&c);
w[sum[i-]+k][sum[i-]+j]=c;
}
}
if(i<=n)scanf("%s",ch);
}
dp[]=;
for(int i=;i<=n+;i++){
for(int j=sum[i-]+;j<=sum[i];j++){
for(int k=sum[i-]+;k<=sum[i-];k++){
dp[j]=min(dp[j],dp[k]+w[k][j]);
}
}
}
int ans=inf;
for(int i=sum[n]+;i<=sum[n+];i++){
ans=min(ans,dp[i]);
}
printf("%d\n",ans);
return ;
}
timus 1210 Kind Spirits(最短路)(动态规划)的更多相关文章
- [NOIP2017] 逛公园 (最短路,动态规划&记忆化搜索)
题目链接 Solution 我只会60分暴力... 正解是 DP. 状态定义: \(f[i][j]\) 代表 \(1\) 到 \(i\) 比最短路长 \(j\) 的方案数. 那么很显然最后答案也就是 ...
- [ZJOI2006]物流运输 最短路 动态规划
Code: 定义状态 $dp[i]$ 为前 $i$ 天的最小代价. 状态转移为:$dp[i]=min(dp[i],dp[j]+spfa(j+1,i)$ 这里 $spfa(i,j)$ 是指 $(i,j) ...
- UVA 10269 Super Mario,最短路+动态规划
这个题目我昨晚看到的,没什么思路,因为马里奥有boot加速器,只要中间没有城堡,即可不耗时间和脚力,瞬间移动不超过L距离,遇见城堡就要停下来,当然不能该使用超过K次...我纠结了很久,最终觉得还是只能 ...
- 1210. Kind Spirits(spfa)
1210 简单模版题 敲个spfa还得瞟下模版.. #include <iostream> #include<cstdio> #include<cstring> # ...
- 【动态规划】【最短路】Codeforces 710E Generate a String
题目链接: http://codeforces.com/problemset/problem/710/E 题目大意: 问写N个字符的最小花费,写一个字符或者删除一个字符花费A,将当前的字符数量翻倍花费 ...
- 【BZOJ1003】物流运输(动态规划,最短路)
[BZOJ1003]物流运输(动态规划,最短路) 题面 Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司 ...
- NOIP 2017 逛公园 - 动态规划 - 最短路
题目传送门 传送门 题目大意 给定一个$n$个点$m$条边的带权有向图,问从$1$到$n$的距离不超过最短路长度$K$的路径数. 跑一遍最短路. 一个点拆$K + 1$个点,变成一个DAG上路径计数问 ...
- 【BZOJ1491】【NOI2007】社交网络(最短路,动态规划)
[BZOJ1491][NOI2007]社交网络(最短路,动态规划) 题面 BZOJ 洛谷 图片是假的,只能到OJ上看 Description 在社交网络(socialnetwork)的研究中,我们常常 ...
- uva 116 Unidirectional TSP(动态规划,多段图上的最短路)
这道题目并不是很难理解,题目大意就是求从第一列到最后一列的一个字典序最小的最短路,要求不仅输出最短路长度,还要输出字典序最小的路径. 这道题可以利用动态规划求解.状态定义为: cost[i][j] = ...
随机推荐
- 戴文的Linux内核专题:01介绍
转自Linux中国 译者按:本文作者戴文.科利尔.约翰逊(Devyn Collier Johnson)今年才19岁,但是他在Linux内核.人工智能.编程语言方面拥有丰富的经验,本文是其在linux. ...
- poj1992 数论
//Accepted 168 KB 969 ms //n!中含有质因数p的个数为t=n/p+n/p^2+n/p^3+... #include <cstdio> #include <c ...
- hdu4597 区间dp
//Accepted 1784 KB 78 ms //区间dp //dp[l1][r1][l2][r2] 表示a数列从l1到r1,b数列从l2到r2能得到的最大分值 // #include <c ...
- SQL优化-索引
(一)深入浅出理解索引结构 实际上,您可以把索引理解为一种特殊的目录.微软的SQL SERVER提供了两种索引:聚集索引(clustered index,也称聚类索引.簇集索引)和非聚集索引(nonc ...
- HttpApplication 类,HttpApplicationState 类
HttpApplication 类 定义 ASP.NET 应用程序中的所有应用程序对象共有的方法.属性和事件.此类是用户在 Global.asax 文件中所定义的应用程序的基类. https://ms ...
- UIkit框架之UIalert(iOS 9之后就不用这个了)
IOS中UIAlertView(警告框)常用方法总结 一.初始化方法 - (instancetype)initWithTitle:(NSString *)title message:(NSString ...
- Interview----2 sum
题目:输入一个已经按升序排序过的数组和一个数字, 在数组中查找两个数,使得它们的和正好是输入的那个数字. 要求时间复杂度是 O(n).如果有多对数字的和等于输入的数字,输出任意一对即可. 例如输入数组 ...
- Makefile总结和反序字符数组,整体右移数组,杨辉三角!
2015.1.23今天我值日 继续接着昨天Makefile的目标开始记录:第一种:有 .PHNOY声明,但是冒号后面没有依赖文件.PHNOY:clean clean://没有依赖文件 rm *.0 t ...
- Intent的简介以及属性详解
一.Intent的介绍 Intent的中文意思是“意图,意向”,在Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述 ...
- 查单神器v1.0 升级 →B站看鬼畜神器v1.0
去年学校实习,我去了一家快递输单公司工作.工作任务就是把运单图片上的内容(寄件人,地址之类)输入到公司的数据库里.每天输单结束后,还要对一些容易错的运单进行排查.单量多的时候一天甚至要查千张以上的运单 ...