【t100】汤姆斯的天堂梦

Time Limit: 1 second
Memory Limit: 128 MB

【问题描述】

汤姆斯生活在一个等级为0的星球上。那里的环境极其恶劣,每天12小时的工作和成堆的垃圾让人忍无可忍。他向往着等级为N的星
球上天堂般的生活。
有一些航班将人从低等级的星球送上高一级的星球,有时需要向驾驶员支付一定金额的费用,有时却又可以得到一定的金钱。
汤姆斯预先知道了从0等级星球去N等级星球所有的航线和需要支付(或者可以得到)的金钱,他想寻找一条价格最低(甚至获得金
钱最多)的航线。
【数据范围】
对于100%的数据 N≤100 Ki≤100。
【样例解释】
如图

【输入格式】

第一行一个正整数N(N≤100),接下来的数据可分为N个段落
每段的第一行一个整数Ki(Ki≤100),表示等级为i的星球有Ki个。
接下来的Ki中第Tij行依次表示与等级为i,编号为j的星球相连
的等级为i-1的星球的编号和此航线需要的费用(正数表示支出
,负数表示收益,费用的绝对值不超过1000)。每行以0结束,每行的航线数≤100。
【输出格式】

输出所需(或所得)费用。正数表示支出,负数表示收益。

Sample Input

3
2
1 15 0
1 5 0
3
1 -5 2 10 0
1 3 0
2 40 0
2
1 125 3 -5 0
2 -19 3 -20 0

Sample Output

-1

【题目链接】:http://noi.qz5z.com/viewtask.asp?id=t100

【题意】

【题解】

题目的样例输入排版没排好;
我在上面的题目描述里面修改好了。可以看一下。。
然后这题就是道动规吧.
阶段就按照等级分就好了;
只能从低等级往高等级走;
肯定没后效性啦;
当初用SPFA写。。。太夸张了

【完整代码】

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 110;
const int INF = 0x3f3f3f3f;

int n;
int f[N][N],ans=INF;

void in()
{
    rei(n);
}

void get_ans()
{
    memset(f,INF,sizeof f);
    f[0][1] = 0;
    rep1(i,1,n)
    {
        int ki;
        rei(ki);
        rep1(j,1,ki)
        {
            int prex,w;
            rei(prex);
            while (prex!=0)
            {
                rei(w);
                f[i][j] = min(f[i][j],f[i-1][prex]+w);
                rei(prex);
            }
        }
    }
    rep1(i,0,100)
        ans = min(ans,f[n][i]);
}

void o()
{
    printf("%d
",ans);
}

int main()
{
    //freopen("F:\rush.txt","r",stdin);
    in();
    get_ans();
    o();
    //printf("
%.2lf sec 
", (double)clock() / CLOCKS_PER_SEC);
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626523.html