HDU4571/2013年长沙赛区Travel in time

Description

Bob gets tired of playing games, leavesAlice, and travels to Changsha alone.YueluMountain, Orange Island, Window of the World, theProvincial Museum etc...are scenic spots Bob wants to visit. However, his timeis very limited, he can’t visit them all.

Assuming that there areN scenicspots in Changsha, Bob defines a satisfaction value Si to each spot. Ifhe visits this spot, his total satisfaction value will plusSi. Bobhopes that within the limited time T, he can start at spotS, visitsome spots selectively, and finally stop at spot E, so that the totalsatisfaction value can be as large as possible. It's obvious that visiting thespot will also cost some time, suppose that it takesCi units of time tovisit spot i ( 0 <= i < N ).

Always remember, Bob can choose to pass bya spot without visiting it (includingS and E), maybe he justwant to walk shorter distance for saving time.

Bob also has a special need which is thathe will only visit the spot whose satisfaction value isstrictly largerthan that of which he visited last time. For example, if he has visited a spotwhose satisfaction value is 50, he would only visit spot whose satisfactionvalue is 51 or more then. The paths between the spots are bi-directional, ofcourse.

 

Input

The first line is an integerW,which is the number of testing cases, and the W sets of data are following.

The first line of each test data containsfive integers:N M T S E. N represents the number of spots, 1< N < 100;M represents the number of paths, 0 < M< 1000; T represents the timelimitation, 0 <T <= 300; S means the spot Bob starts from.E indicates the end spot. (0 <=S, E < N)

The second line of the test data containsNintegers Ci ( 0 <= Ci <= T ), which means the costof time if Bob visits the spoti.

The third line also hasN integers,which means the satisfaction value Si that can be obtained by visitingthe spoti ( 0 <= Si < 100 ).

The next M lines, each line containsthree integersu v L, means there is a bi-directional pathbetween spot u and v and it takes L units of time to walkfrom u to v or fromv to u. (0 <= u, v< N, 0 <= L <=T)

 

 

 

Output

Output case number in the first line (formattedas the sample output).

The second line contains an integer, whichis the greatest satisfaction value.

If Bob can’t reach spotE in T units oftime, you should output just a “0” (without quotation marks).

 

Sample Input

1

4 4 22 0 3

1 1 1 1

5 7 9 12

0 1 10

1 3 10

0 2 10

2 3 10

 

 

Sample Output

Case #1:

21

先用floyd预处理出两两之间的距离,建立有向图,边的方向从满意度低的点到满意度高的点。注意构造两个抽象的起点和终点,表示不访问该点而经过该点。

再跑一遍二维spfa,dist[i][j]表示在j时刻到达i点得到的最大满意度

最后在dist[终点][0~T]以及dist[抽象终点][0~T]中找最大值

 

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;

#define MAXNODE 110
#define MAXTIME 721
#define MAXSATIS 100
#define oo (1<<29)

struct PATH
{
    int v, L;
    PATH(int _v, int _L){v = _v; L = _L;}
};

// Data of the graph
int N, M;
vector<struct PATH> Graph[MAXNODE];

// Start point, end point, time limit
int Sp, Ep, T;

// Satisfaction and time-cost of every sight spot
int s[MAXNODE], c[MAXNODE];

// Satisfaction[i][j] means the max satisfaction on Node i when time is j
int Satisfaction[MAXNODE][MAXTIME];

struct Node
{
    int position, time;
    Node(){}
    Node(int _position, int _time)
    {
        position = _position;
        time     = _time;
    }
};
queue<struct Node> que;

// whether the node is in the queue
bool InQueue[MAXNODE][MAXTIME];

Node Pop_Queue()
{
    struct Node now = que.front();
    que.pop();
    InQueue[now.position][now.time] = false;
    return now;
}
void Push_Queue(Node NewNode)
{
    if (InQueue[NewNode.position][NewNode.time]) return;
    que.push(NewNode);
    InQueue[NewNode.position][NewNode.time] = true;
}
void SPFA()
{
    // initialize
    while (!que.empty()) que.pop();
    for (int i = 0; i < N + 2; ++i)
        for (int j = 0; j <= T; ++j)
        {
            Satisfaction[i][j] = 0;
            InQueue[i][j] = false;
        }

    Push_Queue( Node(N, 0) );
    Satisfaction[N][0] = 0;

    while (!que.empty())
    {
        struct Node now = Pop_Queue();
        // go to another spot - Relaxation operating
        int size = Graph[now.position].size(), NewTime;
        for (int i = 0; i < size; ++i)
        {
            int v = Graph[now.position][i].v;
            NewTime = now.time + Graph[now.position][i].L;
            if (NewTime > T) continue;
            if (Satisfaction[now.position][now.time] + s[v] > Satisfaction[v][NewTime])
            {
                Satisfaction[v][NewTime] = Satisfaction[now.position][now.time] + s[v];
                Push_Queue( Node(v, NewTime) );
            }
        }
    }
}

int Matrix[MAXNODE][MAXNODE];
// Read data
void Init()
{
    scanf("%d %d %d %d %d", &N, &M, &T, &Sp, &Ep);
    for (int i = 0; i < N; ++i) scanf("%d", &c[i]);
    for (int i = 0; i < N; ++i) scanf("%d", &s[i]);
    for (int i = 0; i < N; ++i)
        for (int j = 0; j < N; ++j)
            Matrix[i][j] = (i == j)? (0) : (oo);
    while (M--)
    {
        int u, v, l;
        scanf("%d %d %d", &u, &v, &l);
        Matrix[u][v] = Matrix[v][u] = min(Matrix[u][v], l);
    }
}
void Floyd_Warshall()
{
    for (int k = 0; k < N; ++k)
        for (int i = 0; i < N; ++i)
            for (int j = 0; j < N; ++j)
                Matrix[i][j] = min(Matrix[i][j], Matrix[i][k] + Matrix[k][j]);
}
void Add_Edge(int from, int to, int cost)
{
    Graph[from].push_back(PATH(to, cost));
}
void Build_Graph()
{
    s[N + 1] = 0;
    for (int i = 0; i < N + 2; ++i) Graph[i].clear();

    for (int i = 0; i < N; ++i)
        for (int j = i + 1; j < N; ++j)
            if (Matrix[i][j] < oo)
            {
                if (s[i] > s[j]) Add_Edge(j, i, Matrix[i][j] + c[i]);
                else if (s[i] < s[j]) Add_Edge(i, j, Matrix[i][j] + c[j]);
            }

    //Super source - N
    for (int i = 0; i < N; ++i)
        if (i != Sp && Matrix[Sp][i] < oo)
            Add_Edge(N, i, Matrix[Sp][i] + c[i]);
    Add_Edge(N, Sp, c[Sp]);

    //Super dest - N + 1
    for (int i = 0; i < N; ++i)
        if (i != Ep && Matrix[i][Ep] < oo)
            Add_Edge(i, N + 1, Matrix[i][Ep]);
}
int main()
{
    int Case;
    scanf("%d", &Case);
    for (int i = 1; i <= Case; ++i)
    {
        Init();
        Floyd_Warshall();
        Build_Graph();
        SPFA();
        int maxS = 0;
        for (int t = 0; t <= T; ++t)
        {
            maxS = max (maxS, Satisfaction[N + 1][t]);
            maxS = max (maxS, Satisfaction[Ep][t]);
        }
        printf("Case #%d:\n%d\n", i, maxS);
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值