Kind Spirits

Time limit: 1.0 second
Memory limit: 64 MB
Ivanushka the Fool lives at the planet of 0-level. It's very unpleasant
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.
At each of the i-th level planets there are several
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.
In
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

The first line contains an integer N
(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

should contain the only number — the minimal fee that Ivanushka
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
2
1 15 0
1 5 0
*
3
1 -5 2 10 0
1 3 0
2 40 0
*
2
1 1 2 5 3 -5 0
2 -19 3 -20 0
-1
Problem Author: Leonid Volkov
【分析】咋一看题目,感觉就是最短路,但一看图,很想之前做的数塔,所以就用dp做了。
#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(最短路)(动态规划)的更多相关文章

  1. [NOIP2017] 逛公园 (最短路,动态规划&记忆化搜索)

    题目链接 Solution 我只会60分暴力... 正解是 DP. 状态定义: \(f[i][j]\) 代表 \(1\) 到 \(i\) 比最短路长 \(j\) 的方案数. 那么很显然最后答案也就是 ...

  2. [ZJOI2006]物流运输 最短路 动态规划

    Code: 定义状态 $dp[i]$ 为前 $i$ 天的最小代价. 状态转移为:$dp[i]=min(dp[i],dp[j]+spfa(j+1,i)$ 这里 $spfa(i,j)$ 是指 $(i,j) ...

  3. UVA 10269 Super Mario,最短路+动态规划

    这个题目我昨晚看到的,没什么思路,因为马里奥有boot加速器,只要中间没有城堡,即可不耗时间和脚力,瞬间移动不超过L距离,遇见城堡就要停下来,当然不能该使用超过K次...我纠结了很久,最终觉得还是只能 ...

  4. 1210. Kind Spirits(spfa)

    1210 简单模版题 敲个spfa还得瞟下模版.. #include <iostream> #include<cstdio> #include<cstring> # ...

  5. 【动态规划】【最短路】Codeforces 710E Generate a String

    题目链接: http://codeforces.com/problemset/problem/710/E 题目大意: 问写N个字符的最小花费,写一个字符或者删除一个字符花费A,将当前的字符数量翻倍花费 ...

  6. 【BZOJ1003】物流运输(动态规划,最短路)

    [BZOJ1003]物流运输(动态规划,最短路) 题面 Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司 ...

  7. NOIP 2017 逛公园 - 动态规划 - 最短路

    题目传送门 传送门 题目大意 给定一个$n$个点$m$条边的带权有向图,问从$1$到$n$的距离不超过最短路长度$K$的路径数. 跑一遍最短路. 一个点拆$K + 1$个点,变成一个DAG上路径计数问 ...

  8. 【BZOJ1491】【NOI2007】社交网络(最短路,动态规划)

    [BZOJ1491][NOI2007]社交网络(最短路,动态规划) 题面 BZOJ 洛谷 图片是假的,只能到OJ上看 Description 在社交网络(socialnetwork)的研究中,我们常常 ...

  9. uva 116 Unidirectional TSP(动态规划,多段图上的最短路)

    这道题目并不是很难理解,题目大意就是求从第一列到最后一列的一个字典序最小的最短路,要求不仅输出最短路长度,还要输出字典序最小的路径. 这道题可以利用动态规划求解.状态定义为: cost[i][j] = ...

随机推荐

  1. 建议入门-用ArcMap进行空间查询与空间连接

    1.打开arcmap并导入数据(如本图导入美国地图(usa.mxd)): 2.空间查询操作,在地图上的某片区域点击右键,得到下图,点击identify,此时我在阿拉斯加上面点击的 地图会闪现一下被查询 ...

  2. hashmap and hashtable

    ①继承不同. public class Hashtable extends Dictionary implements Map public class HashMap extends Abstrac ...

  3. 【转发】du命令 实现Linux 某个文件夹下的文件按大小排序

    1. df -lh 2. du -s /usr/* | sort -rn这是按字节排序 3. du -sh /usr/* | sort -rn这是按兆(M)来排序 4.选出排在前面的10个du -s ...

  4. iOS 使用两个tableview的瀑布流

    代码 悦德财富:https://www.yuedecaifu.com 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...

  5. python版恶俗古风自动生成器.py

    python版恶俗古风自动生成器.py """ python版恶俗古风自动生成器.py 模仿自: http://www.jianshu.com/p/f893291674c ...

  6. equals() 与 hashcode() 的区别与联系

    两者都是从Object类继承的方法,Object中equals方法比较的是this和参数传进来的对象的引用地址是否相同,这样的话,equals返回值为true的必要充分条件就是两者指向同一个对象,那么 ...

  7. jQuery get post 碎片(远程html)加载

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. rcc

    一.在STM32中,有五个时钟源,为HSI.HSE.LSI.LSE.PLL.   全名: high  speed  external ①HSI是高速内部时钟,RC振荡器,频率为8MHz. ②HSE是高 ...

  9. 对于我的linux常用命令的说明

    我所列出的linux常用命令中的选项并不是全部的选项,是一些我们经常用到的linux命令及选项

  10. 【LeetCode OJ】Best Time to Buy and Sell Stock II

    Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ We solve this prob ...