URAL 1450 无环图最短路

1450. Russian Pipelines

Time limit: 0.5 second
Memory limit: 64 MB

Background

The last year developed into a lot of trouble for independent Russia. Bad harvest, bird flu and - to crown it all - the President, who decided to raise funds to buy new balalaika and tame bear for the child of his first cousin. These factors (especially the last one) shocked the national economics deeply. Counseled with his friends who wear valenki and ushankas, the President resolved to use a traditional method of state budget reinforcement, which implies a stealing of the Ukrainian gas.

Problem

Russian pipeline system consists of N transfer station, some of which are connected by pipelines. For each of M pipelines the numbers of stations A[i] and B[i], which are connected by this pipeline, and its profitability C[i] are known. A profitability of a pipeline is an amount of dollars, which will be daily yielded by the selling of stolen gas transferring through this pipeline. Each two stations are connected by not more than one pipeline.
The system was made by Ukrainian engineers, who knew exactly, that the gas was transferred from Ukrainian gas fields to Siberia and not the reverse. That is why the pipelines are unidirectional, i.e. each pipeline allows gas transfer from the station number A[i] to the station number B[i] only. More over, if it is possible to transfer the gas from the station X to the station Y (perhaps, through some intermediate stations), then the reverse transfer from Y to X is impossible. It is known that the Ukrainian gas arrives to the starting station number S and should be dispatched to the Siberian buyers on the final station number F.
The President ordered the Government to find a route (i.e. a linear sequence of stations which are connected by pipelines) to transfer the gas from the starting to the final station. A profitability of this route should be maximal. A profitability of a route is a total profitability of its pipelines.
Unfortunately, the President did not consider that some pipelines were plundered long ago, and, as a result, the gas transfer between the starting and the final stations may appear to be impossible...

Input

The first line contains the integer numbers N (2 ≤ N ≤ 500) and M (0 ≤ M ≤ 124750). Each of the next M lines contains the integer numbers A[i], B[i] (1 ≤ A[i], B[i] ≤ N) and C[i] (1 ≤ C[i] ≤ 10000) for the corresponding pipeline. The last line contains the integer numbers S and F (1 ≤ S, F ≤ N; S ≠ F).

Output

If the desired route exists, you should output its profitability. Otherwise you should output "No solution".

Sample

input output
6 7
6 5 10
1 4 11
1 2 4
3 1 5
2 4 5
6 3 1
6 1 3
6 4
17

Hint

In the sample, the desired route is a route 6>3>1>4.
这题就是上交模板的实例了,无环图最短路。不过模板中说的是vis[t]=true,dp[t]=0;而这个AC代码是把源点初始化的,相反了,这个还不知道啥回事。

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <list>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define PI acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define sca(a) scanf("%d",&a)
#define sc(a,b) scanf("%d%d",&a,&b)
#define pri(a) printf("%d\n",a)
#define lson i<<1,l,mid
#define rson i<<1|1,mid+1,r
#define MM 1000005
#define MN 650
#define INF 1000000009
#define eps 1e-7
using namespace std;
typedef long long ll;
ll e[MN][MN],dp[MN];
int n,m,st,en;
bool vis[MN];
ll DAG_path(int x)
{
    if(vis[x]) return dp[x];
    for(int i=1;i<=n;i++)
        if(e[i][x]) dp[x]=max(dp[x],DAG_path(i)+e[i][x]);
    vis[x]=true;
    if(dp[x]<0) dp[x]=-INF;
    return dp[x];
}
void init()
{
    mem(vis,0); mem(dp,-1);
    vis[st]=1,dp[st]=0;
}
int main()
{
    int u,v,w;
    sc(n,m);
    while(m--)
    {
        scanf("%d%d%d",&u,&v,&w);
        e[u][v]=w;
    }
    sc(st,en);
    init();
    DAG_path(en);
    if(dp[en]<0) puts("No solution");
    else printf("%lld\n",dp[en]);
    return 0;
}



你可能感兴趣的:(URAL 1450 无环图最短路)