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] = ...
随机推荐
- 分布式一致性原理—CAP
背景 随着分布式事务的出现,传统的单机事务模型(ACID)已经无法胜任,尤其是对于一个高访问量.高并发的互联网分布式系统来说. 如果我们要求严格一致性,很可能就需要牺牲掉系统的可用性,反之亦然.但两者 ...
- 多线程第一次亲密接触 CreateThread与_beginthreadex本质区别
本文将带领你与多线程作第一次亲密接触,并深入分析CreateThread与_beginthreadex的本质区别,相信阅读本文后你能轻松的使用多线程并能流畅准确的回答CreateThread与_beg ...
- php中的include()的使用技巧
php中的include()的使用技巧 include() 语句包括并运行指定文件. 以下文档也适用于 require().这两种结构除了在如何处理失败之外完全一样.include() 产生一个警告而 ...
- Hash(哈希)
一.基本概念 Hash,一般翻译做"散列",也有直接音译为"哈希"的,就是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定长度的 ...
- PAT 10-2 删除字符串中的子串
今天发一个不完全对(通过garbageMan园友的帮忙,现已全对)的代码,(/*后两用例未通过,一时看不出问题在哪,*/)切入正题 /* Name: Copyright: Author: Date: ...
- 事件委托 EventHandler
事件就是当对象或类状态发生改变时,对象或类发出的信息或通知.发出信息的对象或类称为"事件源",对事件进行处理的方法称为"接收者",通常事件源在发出状态改变信息时 ...
- Intellij IDEA Help
https://www.jetbrains.com/idea/help/intellij-idea.html https://www.jetbrains.com/idea/help/creating- ...
- The ShortCuts in the ADT (to be continued)
1. automatically add all the namespace which need to be include in the class. ctrl+shift+o
- 沙盒密探——可实现的js缓存攻击
我们描述了第一次完全运行在浏览器端的微结构单通道攻击.与其他参和这种类型的相反,这种攻击不再需要攻击者在肉鸡上安装任何软件,为了让攻击更容易,肉鸡仅仅需要浏览哪些攻击者控制的不被信任的网页内容.这会让 ...
- XML文件的读取、序列化和反序列化操作
public class XmlHelper { //从xml中获取MsgType public static string XMLSelect(string XML) { XmlDocument x ...