timus 1210 Kind Spirits(最短路)(动态规划)

Posted 贱人方

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了timus 1210 Kind Spirits(最短路)(动态规划)相关的知识,希望对你有一定的参考价值。

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.
Problem illustration

Sample

inputoutput
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=1005;
const int M=50000;
int power(int a,int b,int c){int ans=1;while(b){if(b%2==1){ans=(ans*a)%c;b--;}b/=2;a=a*a%c;}return ans;}
int w[N][N],vis[N];
int n,m,k,c,s=1;
int sum[N],dp[N];
int main()
{
    memset(w,inf,sizeof(w));
    memset(dp,inf,sizeof(dp));
    sum[1]=1;
    char ch[2];
    scanf("%d",&n);
    for(int i=2;i<=n+1;i++){
        scanf("%d",&m);
        sum[i]=sum[i-1]+m;
        for(int j=1;j<=m;j++){
            while(~scanf("%d",&k)&&k){
                scanf("%d",&c);
                w[sum[i-2]+k][sum[i-1]+j]=c;
            }
        }
        if(i<=n)scanf("%s",ch);
    }
    dp[1]=0;
    for(int i=2;i<=n+1;i++){
        for(int j=sum[i-1]+1;j<=sum[i];j++){
            for(int k=sum[i-2]+1;k<=sum[i-1];k++){
                dp[j]=min(dp[j],dp[k]+w[k][j]);
            }
        }
    }
    int ans=inf;
    for(int i=sum[n]+1;i<=sum[n+1];i++){
        ans=min(ans,dp[i]);
    }
    printf("%d\\n",ans);
    return 0;
}
View Code

 

以上是关于timus 1210 Kind Spirits(最短路)(动态规划)的主要内容,如果未能解决你的问题,请参考以下文章

BANDAI SPIRITS将在北京举办面向奥特曼和假面骑士忠实粉丝的特别活动:2021北京英雄“魂”展

timus1745题解

Timus[1091. Tmutarakan Exams]

timus 1033 Labyrinth(BFS)

Timus 1146. Maximum Sum

1923. Scary Politics (timus) (dfs) search