Algorithm improves road and route time limits: 1.0s memory limit: 256.0MBProblem 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 road I or route I is connected to town aI(1<=a_i<=t) and BI(1<=bI<=T) at a cost of CI。 Every road, C.IThe range is 0<=cI<=10,000; because of the strange operation strategy, the C of each routeImay be negative, that is, -10,000<=c.I<=10,000.
Each road is two-way, positive and reverse costs are the same, are non-negative.
Each route is based on the input of aIand bIProceed from aI->bIOne-way pass. In fact, if there is now a route from aIto BIWords, then that means there's definitely no pass plan from BIBack to aI。
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.
The next P-line, describing the route information, contains three integers per line, representing ai, Bi , and Ci, respectively.
The output format outputs T line, which represents the minimum cost from town s to each city, and if not, output no PATH. Sample Input 6 3 3 4
1 2 5
3 4 5
5 6 10
3 5-100
4 6-100
1 3-10 Example Output no PATH
NO PATH
5
0
-95
-100 data 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.
The first time to submit the time scored 90, there are two sets of test data timeout, because know SPFA still have optimization, so use SLF optimization, specific optimization see blog http://www.cnblogs.com/pony1993/archive/2012/09/07/2675654.html. I'm not going to say much.
Data water, let this problem change is very simple.
Test results:
Directly on the code:
#include <cstdio> #include <cstring> #include <deque> #define MAX 150100#define INF 1000000000using namespace std; struct node{int to, W, next;} Edge[max];int DIS[MAX/4], HEAD[MAX/4], C[MAX/4]; bool Visited[max/4];bool SPFA (int s, int n) {for (int i = 0; I <= n ; ++i) {Dis[i] = INF; C[i] = 0; Visited[i] = false;} Dis[s] = 0; Visited[s] = True;d eque<int> que; Que.push_front (s); while (!que.empty ()) {int k = Que.front (); que.pop_f Ront (); visited[k] = false; C[k]++;if (C[k]>n) {return false;} for (int i = head[k]; i =-1; i = edge[i].next) {if (dis[edge[i].to] > DIS[K]+EDGE[I].W) {dis[edge[i].to] = dis[k]+edge[ I].W; if (Que.empty ()) {Que.push_front (edge[i].to); visited[edge[i].to] = true;} else if (!visited[edge[i].to]) {if (dis[edge[i].to] > Dis[que.front ()]) {que.push_back (edge[i].to);} Else{que.push_front (edge[i].to);} Visited[edge[i].to] = true;}}} return true;} int main () {int t,r,p,s;while (scanf ("%d%d%d%d", &t,&r,&p,&s)!=eof) {memset (head,-1,sIzeof (head)); int index = 0; for (int i = 0; i < R; ++i) {int ai,bi,ci;scanf ("%d%d%d", &ai,&bi,&ci); edge[ind ex].to = bi; edge[index].w = ci; edge[index].next = Head[ai]; Head[ai] = index++; edge[index].to = ai; edge[index].w = ci ; edge[index].next = Head[bi]; Head[bi] = index++;} for (int i = 0; i < P; ++i) {int ai,bi,ci;scanf ("%d%d%d", &ai,&bi,&ci); edge[index].to = bi; edge[index].w = ci; edge[index].next = Head[ai]; Head[ai] = index++;} SPFA (s,t); for (int i = 1; I <= t; ++i) {if (Dis[i] >= INF) {puts ("NO PATH");} else{printf ("%d\n", Dis[i]);}}} return 0;}
with June
Blue Bridge Cup algorithm to improve road and route out of AC, SPFA algorithm SLF optimization, test data or comparison of water, seemingly no heavy side