URAL 1450 无环图最短路
来源:互联网 发布:geo数据库使用方法 编辑:程序博客网 时间:2024/05/16 00:30
1450. Russian Pipelines
Time limit: 0.5 second
Memory limit: 64 MB
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
6 76 5 101 4 111 2 43 1 52 4 56 3 16 1 36 4
17
Hint
In the sample, the desired route is a route 6>3>1>4.
#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-7using 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;}
0 0
- URAL 1450 无环图最短路
- spfa最短路 [URAL-1930]
- 【二分答案&&最短路】URAL
- ural 1325. Dirt(最短路)
- URAL 2034 Caravans (最短路 + 二分)
- ural 1450
- ural 1016 Cube on the Walk 状态压缩最短路
- URAL 1980 Road to Investor(二分+最短路)
- URAL 1980 Road to Investor 二分+最短路
- URAL 1934 Black Spot --- 简单最短路变形
- URAL 2005. Taxi for Programmers (最短路 数学啊)
- ural 1325 Dirt(bfs最短路+优先队列)
- URAL
- 【ural】
- URAL
- URAL
- URAL
- URAL
- ZOJ 3492//见过骰子 就有思路的说
- jQuery Validate
- 20140409 Complex again(for lab)
- KM算法
- YARN/MRv2 Node Manager深入剖析—整体架构
- URAL 1450 无环图最短路
- poj 3252 Round Numbers
- BNU 29379斩
- ubuntu12.04 安装2013QQ
- windows7,windows8,ubuntu三系统64位安装
- 单例模式的实现技巧
- How advertising cookies let observers follow you across the web
- jdbc操作mysql
- 阿里云CentOS 6.3 64位 配置PHP环境