P1476 rest of Xiaodai

Title Description

When we put to the test in the examination room (torture?), The Xiao Dai is a leisurely (Qianbian) to a play called "The initial dream" of the game. Game description of an aspiring teenager called pass through the confrontation of the legendary devil chinesesonic story in a different time and space. Xiao Dai found the story of the game design process is very complex, it has a lot of branches of the story, but the story is different branches can be carried out at the same time, so the game can be the end point of the story and the story of the composition of some plot must be in some to continue to develop after the end of a specific plot. In order to experience the integrity of the game, Xiao Dai decided to see all the branches of the story - to complete all the tasks. But this will not delay Xiaodai valuable sleep time? So I asked you to solve this problem.

Input Format

Xiao Dai will give you a list of story flow and closing conditions,

Wherein a first line number n (0 <n <100), a total of n represents the end point of the plot,

A second row number m (0 <m <= 120), expressed in m different story,

The following m-th row in each row of three numbers i (0 <i <= 100), j (0 <j <= 100), k (0 <k <= 1000), indicates from the plot end point i must complete a cost k story time to reach the end point of the story j.

Output Format

To tell you how much time Xiaodai complete the entire game and need at least to go through all the possible plot end point (in ascending order output).

Sample input and output

Input # 1
4
5
1 2 2
2 3 2
3 5 3
1 4 3
4 5 3
Output # 1
7
1 2 3 5



#include<bits/stdc++.h>
using namespace std;
#define N 120+1
int f[N][N],n,m;
int main()
{
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int x,y,z;
        cin>>x>>y>>z;
        f[x][y]=z;
    }
    n++;
    for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(i!=j&&i!=k&&j!=k&&f[i][k]&&f[k][j])
                    f[i][j]=max(f[i][j],f[i][k]+f[k][j]);
    cout<<f[1][n]<<endl;
    for(int i=1;i<=n;i++)
        if(f[1][i]+f[i][n]==f[1][n])
            cout<<i<<" ";
    cout<<endl;
}

 

Guess you like

Origin www.cnblogs.com/jzxnl/p/11404088.html