Directory
1 Problem Description
2 Solutions
1 problem description Problem Description
Farmer John is studying the milk distribution contract for a new area. He intends to distribute milk to T-town (labeled 1. T), these towns are labeled by the R Bar (1). R) is labeled (1) for the road and P-bar. P) are connected by the route.
Each highway I or route I represents a link between town AI (1<=a_i<=t) and Bi (1<=bi<=t) at the cost of CI. Each road, CI range is 0<=ci<=10,000; because of the strange operation strategy, the CI of each route may be negative, namely -10,000<=ci<=10,000.
Each road is two-way, positive and reverse costs are the same, are non-negative.
Each route is a one-way pass from Ai->bi based on the AI and bi entered. In fact, if there is now a route from AI to Bi, then there is definitely no way to get back to AI from bi.
Farmer John wants to send his good milk from the distribution center to all the towns, of course, the smaller the price the better, can you help him? The distribution center is located in town S (1<=s<=t).
Input Format
The first line of input contains four integer t,r,p,s separated by a space.
The next R line, describing the road information, contains three integers per line, representing Ai,bi and CI respectively.
Next P line, describes the route information, each line contains three integers, respectively, representing Ai,bi and CI.
output Formatoutput T lines, representing the minimum cost from town s to each city, if not, output no PATH. Sample Input6 3 3 4
1 2 5
3 4 5
5 6
3 5-100
4 6-100
1 3-10Sample OutputNO PATH
NO PATH
5
0
-95
-100data size and conventions
For 20% of data,t<=100,r<=500,p<=500;
For 30% of data,r<=1000,r<=10000,p<=3000;
For 100% of data, 1<=t<=25000,1<=r<=50000,1<=p<=50000.
2 Solutions
The main problem is to examine the shortest path, where the time efficiency of the best algorithm for the SPFA algorithm, but the following code in the Blue Bridge system score of 30 or 35 points, Reason: Run timeout, and the same method, with C to achieve in the Blue Bridge system score 95 or 100 points, c implementation of code see At the end of this article, reference 1.
If there is a classmate Java version Code evaluation scores more than 50 points of the students, but also look for reference code, not very grateful ~
The specific code is as follows:
Importjava.util.ArrayList;ImportJava.util.Scanner; Public classMain { Public Static intT, R, P, S; Public StaticArraylist<edge>[] map; Public Static int[] distance; Static classEdge { Public intA//the beginning of the side Public intb//the end of the edge Public intV//weighted value of the edge PublicEdgeintAintBintv) { This. A =A; This. B =b; This. v =v; }} @SuppressWarnings ("Unchecked") Public voidinit () {map=NewArraylist[t + 1]; Distance=New int[T + 1]; for(inti = 0;i <= t;i++) {Map[i]=NewArraylist<edge>(); Distance[i]=Integer.max_value; } } Public voidSPFA () {ArrayList<Integer> Town =NewArraylist<integer>(); Distance[s]= 0; Town.add (S); int[] Count =New int[T + 1]; Boolean[] visited =New Boolean[T + 1]; Count[s]++; Visited[s]=true; while(Town.size ()! = 0) { intStart = Town.get (0); Town.remove (0); Visited[start]=false; for(inti = 0;i < Map[start].size (); i++) {Edge to=Map[start].get (i); if(DISTANCE[TO.B] > Distance[start] +to.v) {DISTANCE[TO.B]= Distance[start] +to.v; if(!VISITED[TO.B]) {Town.add (TO.B); VISITED[TO.B]=true; COUNT[TO.B]++; if(COUNT[TO.B] > T)//a negative ring appears at this time return; } } } } } Public voidGetResult () {SPFA (); for(inti = 1;i <= t;i++) { if(Distance[i] = =integer.max_value) System.out.println ("NO PATH"); ElseSystem.out.println (Distance[i]); } } Public Static voidMain (string[] args) {main test=NewMain (); Scanner in=NewScanner (system.in); T=In.nextint (); R=In.nextint (); P=In.nextint (); S=In.nextint (); Test.init (); for(inti = 1;i <= r;i++) { intA =In.nextint (); intb =In.nextint (); intv =In.nextint (); Map[a].add (NewEdge (A, B, v)); Map[b].add (NewEdge (b, A, v)); } for(inti = 1;i <= p;i++) { intA =In.nextint (); intb =In.nextint (); intv =In.nextint (); Map[a].add (NewEdge (A, B, v)); } test.getresult (); }}
Resources:
1. Blue Bridge Cup algorithm to improve the road and route out of AC, SPFA algorithm SLF optimization, test data or comparison of water, seemingly no heavy side
Algorithm Note _165: algorithm to improve road and route (Java)