PAT Advanced 1111. Online Map (30)

问题描述:

1111. Online Map (30)

时间限制
300 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N (2 <= N <= 500), and M, being the total number of streets 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 are the indices (from 0 to N-1) of the two ends of the street; one-way is 1 if the street is 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 in the next line print the fastest path with total time T:

Time = T: source -> w1 -> ... -> destination

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

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

Distance = D; Time = T: source -> u1 -> ... -> destination

Sample Input 1:
10 15
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 = 6: 3 -> 4 -> 8 -> 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 = 4: 3 -> 2 -> 5

我开始复习advanced里的题了。。。

没有什么最短路问题是不能用 堆优化的dijkstra算法 解决的,如果有,就用两次。。。

  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
 59
 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
120
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
152
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>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>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",&c1,&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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值