PAT advanced 1111. Online Map (30)

Source: Internet
Author: User
Tags bool printf time limit

Problem Description:

1111. Online Map ( time limit)
Memory Limit 65536 KB
Code length limit 16000 B
Program Standard author CHEN, Yue

Input position and a destination, an online map can recommend several paths. Now your job are to recommend, paths to your user:one are the shortest, and the other is the fastest. It is guaranteed, a path exists for any request.

Input Specification:

Each input file contains the one test case. For each case, the first line gives-positive integers n (2 <= n <=), and M, being the total number of the street s intersections on a map, and the number of streets, respectively. Then M. lines follow, each describes a street in the format:

V1 V2 One-way length time

where V1 and V2 is the indices (from 0 to N-1) of the "The" of the street; one-way is 1 if the street was one-way from V1 to V2, or 0 if not; length is the length of the street; And time is the time taken to pass the street.

Finally a pair of source and destination is given.

Output Specification:

For each case, first print the shortest path from the source to the destination with distance D in the format:

Distance = D:source, v1---Destination

Then on the next line print the fastest path with total time T:

Time = T:source, W1---destination

In case the shortest path isn't unique, output the fastest one among the shortest paths, which is guaranteed to be unique . In case the fastest path isn't unique, output the one that passes through the fewest intersections, which is guaranteed t o be unique.

In case the shortest and the fastest paths is identical, print them in one line in the format:

Distance = D; Time = T:source-u1-Destination Sample Input 1:

Ten
0 1 0 1 1
8 0 0 1 1
4 8 1 1 1
3 4 0 3 2
3 9 1 4 1
0 6 0 1 1
7 5 1 2 1 8
5
1 2 1 2 3 0 2 2
2 1 1 1 1
1 3 0 3 1
1 4 0 1 1
9 7 1 3 1
5 1 0 5 2
6 5 1 1 2
3 5
Sample Output 1:
Distance = 8, 4, 6:3, 5 time
= 3:3, 1, 5
Sample Input 2:
7 9
0 4 1 1 1
1 6 1 1 3
2 6 1 1 1
2 5 1 2 2
3 0 0 1 1
3 1 1 1 3
3 2 1 1 2 4
5 0 2
2 6 5 1 1 2
3 5
Sample Output 2:
Distance = 3; Time = 5 4:3, 2

I began to review the problem in advanced ...

There is no short-circuit problem can not be solved with heap optimization Dijkstra algorithm, if there is, use two times ...

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 5
 9 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 12
0 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 153 154 155 156 157 
#include <bits/stdc++.h> using namespace std;
	struct node {int id;
	int Len;
	int Tim;
	int jump;
int pid;
} No; struct CMP1 {bool operator () (const node & e1,const Node & E2) Const {if (E1.len!=e2.len) return E1.len&gt
		; E2.len;
	else return e1.tim>e2.tim;
}
}; struct CMP2 {bool operator () (const node & e1,const Node & E2) Const {if (E1.tim!=e2.tim) return E1.tim&gt
		; e2.tim;
	else return e1.jump>e2.jump;
}
};
vector<vector<node> > V;
	int main () {//Ios::sync_with_stdio (FALSE);//Freopen ("Data.txt", "R", stdin);
	int n,m,k,c1,c2,x,t,l,e11,e12,e13,e21,e22,e23;
	scanf ("%d%d", &n,&m);
	V.resize (n);
	vector<int> vb (n,-1);
	No.pid=0;
	No.jump=0;
	for (; m--;)
		{scanf ("%d%d%d%d%d", &c1,&c2,&k,&no.len,&no.tim);
		NO.ID=C2;
		V[c1].push_back (no);
			if (!k) {no.id=c1;
		V[c2].push_back (no);
	}} scanf ("%d%d", &AMP;C1,&AMP;C2);
	
	Stack<int> s1,s2;
	NO.ID=C1;
	No.jump=0;
	no.len=0; No.tim=0;
	NO.PID=C1;
	Priority_queue<node,vector<node>,cmp1> Q1;
	Q1.push (no); for (;!
	Q1.empty ();)
		{no=q1.top ();
		X=no.id;
		L=no.len;
		T=no.tim;
		K=no.jump;
			if (vb[x]<0) {vb[x]=no.pid;
				if (X==C2) {E11=no.len;
				E12=no.tim;
				E13=no.jump;
			Break
				} for (int i=0;i<v[x].size (); i++) {no.pid=x;
				No.id=v[x][i].id;
				No.len=l+v[x][i].len;
				No.tim=t+v[x][i].tim;
				no.jump=k+1;
			Q1.push (no);
	}} q1.pop ();
	
	} for (int i=c2;i!=c1;i=vb[i]) S1.push (i);
	Vb.clear ();
	Vb.resize (n,-1);
	NO.ID=C1;
	No.jump=0;
	no.len=0;
	no.tim=0;
	NO.PID=C1;
	Priority_queue<node,vector<node>,cmp2> Q2;
	Q2.push (no); for (;!
	Q2.empty ();)
		{no=q2.top ();
		X=no.id;
		L=no.len;
		T=no.tim;
		K=no.jump;			
			if (vb[x]<0) {vb[x]=no.pid;
				if (X==C2) {E21=no.len;
				E22=no.tim;
				E23=no.jump;
			Break
				} for (int i=0;i<v[x].size (); i++) {no.pid=x;
				No.id=v[x][i].id;
No.len=l+v[x][i].len;				No.tim=t+v[x][i].tim;
				no.jump=k+1;
			Q2.push (no);
	}} q2.pop ();
	
	} for (int i=c2;i!=c1;i=vb[i]) S2.push (i); if (e11==e21&&e12==e22&&e13==e23) {printf ("Distance =%d;
		Time =%d: ", e11,e12);
		printf ("%d", C1); for (;!
	S1.empty (); S1.pop ()) printf ("-%d", s1.top ());
		} else {printf ("Distance =%d:", E11);
		printf ("%d", C1); for (;!
		S1.empty (); S1.pop ()) printf ("-%d", s1.top ());
		printf ("\ n");
		
		printf ("Time =%d:", E22);
		printf ("%d", C1); for (;!	
	S2.empty (); S2.pop ()) printf ("-%d", s2.top ());
} return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.