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. Spring学习笔记之Constructor-based or setter-based DI?

    如果是强制依赖,那么使用构造器注入,如果是可选依赖,那么使用set方法注入.Spring鼓励构造器注入,可以确保依赖项不为null, Since you can mix constructor-bas ...

  2. [转]Android进程与线程基本知识

    转自:http://www.cnblogs.com/hanyonglu/archive/2012/04/12/2443262.html 本文介绍Android平台中进程与线程的基本知识. 很早的时候就 ...

  3. iOS中类方法的作用

    类方法,这意味着你将它发送给类,而不是对象实例. 因为不是发送给一个实例,所以你不能使用任何实例变量,你只能做一些通用性的事情. 实际上类方法只用于两种情况: 1.创建事物,比如创建一个特殊格式的字符 ...

  4. Format a Hard Drive in Csharp C#格式化总结

    using System; using System.Diagnostics; using System.IO; using System.Linq; using System.Management; ...

  5. 乌龟棋(noip2010)

    分析:该题是经典的动态规划题目. 题目中涉及到卡片数.卡片分4类.格子数等若干信息,又每张卡片仅能使用一次.求到达终点最多能能获得多少分. 从题目中可知卡片的使用顺序影响最终得分,我们可知状态转移和使 ...

  6. 解决办法:在指定的 DSN 中,驱动程序和应用程序之间的体系结构不匹配 问题解决 SQLSTATE=IM014

    环境:64位win7,64位MySql 解决办法:32为和64位ODBC都安装上即可.

  7. (spring-第6回【IoC基础篇】)BeanDefinition——实例化Bean之前的第一大利器。

    上节讲了Bean实例化的内部机制,这里再复述一遍: ResourceLoader从系统中加载XML配置信息,并由Resource来表示. BeanDefinitionReader从Resource中读 ...

  8. linux命令:mkdir

    1.介绍: mkdir用来创建目录,要求创建目录的用户在当前目录具有写的权限. 2.命令格式: mkdir [选项] 目录 3.命令参数 -m, --mode=模式,设定权限<模式> (类 ...

  9. HDU1272-小希的迷宫(并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 #include<cstdio> #include<cstring> u ...

  10. HDU1166-敌兵布阵(线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 #include<cstdio> #include<string> #i ...