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. 到目前为止,Linux下最完整的Samba服务器配置攻略 (转)

    http://blog.chinaunix.net/uid-23069658-id-3142052.html 安装平台为UBUNTU 14.04,直接软件中心安装samba, service smb ...

  2. [转]android Intent机制详解

    转自:http://blog.csdn.net/t12x3456/article/details/7688154 1.什么是Intent Intent是一种运行时绑定(run-time binding ...

  3. ARC以及MRC中setter方法

    ARC以及MRC中setter方法的差异 有时候,你会需要重写setter或者getter方法,你知道么,ARC与MRC的setter方法是有着差异的呢. 先看下MRC下的setter方法: 在看下A ...

  4. 如何由Height Map生成Normal Map

    转自:http://www.cnblogs.com/cxrs/archive/2009/11/01/1594155.html Nvidia和ATI都有相应的工具把Heightmap转成NormalMa ...

  5. GPRS Sniffing Tutorial

    - Download sources into ~/gprs_sniffer git clone git://git.osmocom.org/osmocom-bb.git git clone git: ...

  6. loadrunner录制时弹出invalid application path!please check if application exists对话框

    问题:oadrunner录制时弹出invalid application path!please check if application exists对话框 原因:IE浏览器地址不对,需要手动重新选 ...

  7. 使用struts2的<s>标签出错

    15:org.apache.struts2.views.jsp.ActionTag 16:JSP 17:18:19:executeResult Server: Resin/3.1.4a Content ...

  8. 数据结构 《2》----基于邻接表表示的图的实现 DFS(递归和非递归), BFS

    图通常有两种表示方法: 邻接矩阵 和 邻接表 对于稀疏的图,邻接表表示能够极大地节省空间. 以下是图的数据结构的主要部分: struct Vertex{ ElementType element; // ...

  9. 作业5-需求分析(EX:南通大学成绩录入系统)

    产品利益相关者: 用户:南通大学各任课老师. 顾客:南通大学(教务处) 工程师:系统的开发,测试,维护,推广人员等 竞争性需求分析: N(需求) A(做法) B(好处) C(竞争) D(推广) 基础性 ...

  10. linux邮件服务器postfix配置实例

    linux邮件服务器postfix配置实例(超级详细!!!) 2013-03-13 13:30:21 标签:邮件服务器 linux 1. 系统安装:1)centos4.3 选上MAIL组件里的全部.2 ...