FatMouse and JavaBean II

Description

So this is the second version of FatMouse and his favorite food, JavaBean -- boy he just cannot have enough of it, can he? Again FatMouse was lucky enough to find a map of another storehouse which contains JavaBean. The map showed several scattered rooms storing JavaBeans and the rooms were connected by some tunnels. Amount of JavaBeans in each room and the length of each tunnel between any pair of rooms were marked on the map. After some negotiations FatMouse finally had a cat agree to clear two of the rooms' guards for him to enter and leave, under the condition that he had to leave as soon as possible from the other end. Now he comes to you with his map, asking if you could tell him the path for getting the maximum amount of JavaBeans.

Input:

Input consists of several test cases.
For each test case, the first line contains 4 positive integers: N (<= 500) - the number of rooms (and the rooms are numbered from 0 to N-1), M - the number of tunnels, Rm1 and Rm2 - the rooms that FatMouse may enter or leave. The next line contains N integers, where the i-th integer is the number of JavaBeans at the i-th room. Then M lines follow, each describes a tunnel with three integers R1, R2 and L, which are the pair of rooms connected by a tunnel and the length of that tunnel, respectively.
It is guaranteed that there exists at least one path from Rm1 to Rm2.

Output:

For each test case, print in the first line two numbers: the number of different paths that satisfy his agreetment with the cat, and the maximum amount of JavaBeans FatMouse can possibly take. Then in the second line, print the rooms on the path which bring him the maximum profit, from Rm1 to Rm2. It is guaranteed that this path is unique since I am too lazy to write up a special judge program :)
All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

Sample Input:
5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1
1 0 0 0
2
Sample Output:
2 4
0 1 2
1 2
0



这道题总感觉时间好像会超限,但是交上去只用了0ms,我没有搞懂。

题意:

需要拿到价值最多,但是走的路是最少的,输出最短路径的条数和价值可以取得最大的,

第二行打印路径。

我先是求出最短路径的长度,然后在搜索符合最短路径的条数,并统计能得到价值最大值和路径。

#include <stdio.h>
#include <vector>
#include <string.h>
#define CLR( a ) memset ( a, 0, sizeof ( a ) )
#define INF 0x7ffffff
const int maxn = 505;
int val[maxn], vis[maxn], path[maxn], ans[maxn];
int time, value, end, cnt, map[maxn][maxn], n, lgh;
void init ( )
{
    CLR ( vis );
    for ( int i = 0; i < n; i ++ )  //给map初始化最大值,求最短路径
        for ( int j = 0; j < n; j ++ )
            map[i][j] = INF;
}
void dfs ( int u, int k, int vl, int dis )
{
    if ( dis > lgh )    //当长度超过最短路径时剪枝
        return ;
    vl = vl+val[u]; //将价值加起来
    path[k ++] = u; //保存路径
    if ( u == end )
    {
        time ++;    //达到最后就证明这也是一条最短路
        //注意只要是一条最短路径time就要加1
        if ( vl > value && dis <= lgh )
        {
            value = vl; //保存价值最大值和路径
            cnt = k;
            memcpy ( ans, path, sizeof ( int )*k );
        }
        return ;
    }
    for ( int i = 0; i < n; i ++ )
    {
        if ( vis[i] == 0 && map[u][i] != INF )
        {
            vis[i] = 1;
            dfs ( i, k, vl, dis+map[u][i] );
            vis[i] = 0; //回溯
        }
    }
}
int Dijkstra ( int begin, int end ) //求出begin-end的最短路长度
{
    int final[maxn], L[maxn], k;
    for ( int i = 0; i < n; i ++ )
    {
        L[i] = map[begin][i];
        final[i] = 0;
    }
    final[begin] = 1;
    L[begin] = 0;
    for ( int i = 0; i < n; i ++ )
    {
        int min = INF;
        for ( int j = 0; j < n; j ++ )
        {
            if ( final[j] == 0 && L[j] < min )
            {
                min = L[j];
                k = j;
            }
        }
        final[k] = 1;
        //printf ( "%d %d\n", k, min );
        if ( k == end )
            return min;
        for ( int j = 0; j < n; j ++ )
        {
            if ( final[j] == 0 && min+map[k][j] < L[j] )
                L[j] = min+map[k][j];
        }
    }
    return INF;
}
int main ( )
{
    int m, begin, u, v, l;
    while ( ~ scanf ( "%d%d%d%d", &n, &m, &begin, &end ) )
    {
        init ( );
        for ( int i = 0; i < n; i ++ )
            scanf ( "%d", &val[i] );
        while ( m -- )
        {
            scanf ( "%d%d%d", &u, &v, &l );
            if ( map[u][v] > l )    //保存最小值
                map[u][v] = map[v][u] = l;
        }
        if ( begin == end ) //起点终点一起无法求最短路径,所以只需要直接输出就行了
        {
            printf ( "%d %d\n", 1, val[begin] );
            printf ( "%d\n", begin );
            continue ;
        }
        time = value = 0;
        lgh = Dijkstra ( begin, end );  //保存最短路径
        vis[begin] = 1; //第一个标记无法用
        dfs ( begin, 0, 0, 0 );
        printf ( "%d %d\n", time, value );
        //输出有几条路径可以达到end且都是最短路径,价值最大
        printf ( "%d", ans[0] );    //输出最优路径
        for ( int i = 1; i < cnt; i ++ )
            printf ( " %d", ans[i] );
        printf ( "\n" );
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值