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.
Problem Author: Dmitry Kovalioff, Ilya Grebnov, Nikita Rybak
Problem Source: Timus Top Coders: Second Challenge
***************************************************************************************
单源路径求最大权值的路径spfa,没技巧
***************************************************************************************
1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<cmath> 5 #include<queue> 6 #include<stack> 7 using namespace std; 8 int head[1001]; 9 int n,m,s,e; 10 int h1,h2,w,I; 11 struct edge 12 { 13 int b,next,v; 14 15 }ed[124756]; 16 bool in[1001]; 17 int dis[1001]; 18 void add(int i,int j,int v)//加点,链式前向星 19 { 20 ed[I].b=j; 21 ed[I].v=v; 22 ed[I].next=head[i]; 23 head[i]=I++; 24 } 25 int spfa(int s,int e)//求最大权值 26 { 27 memset(in,false,sizeof(in)); 28 memset(dis,0,sizeof(dis)); 29 queue<int>Q; 30 Q.push(s); 31 in[s]=true; 32 dis[s]=0; 33 while(!Q.empty()) 34 { 35 int ds=Q.front(); 36 Q.pop(); 37 in[ds]=false; 38 for(int h=head[ds];h!=-1;h=ed[h].next) 39 { 40 int df=ed[h].b; 41 if(dis[df]<dis[ds]+ed[h].v) 42 dis[df]=dis[ds]+ed[h].v; 43 if(!in[df]) 44 { 45 Q.push(df); 46 in[df]=true; 47 } 48 } 49 } 50 return dis[e]; 51 52 } 53 int main() 54 { 55 cin>>n>>m; 56 I=0; 57 memset(head,-1,sizeof(head)); 58 for(int i=1;i<=m;i++) 59 { 60 cin>>h1>>h2>>w; 61 add(h1,h2,w); 62 63 } 64 cin>>s>>e; 65 int gs=spfa(s,e); 66 if(gs==0) 67 cout<<"No solution"<<endl; 68 else 69 cout<<gs<<endl; 70 return 0; 71 72 }
转载于:https://www.cnblogs.com/sdau--codeants/p/3263986.html