1. 程式人生 > >1131 Subway Map (30 分)

1131 Subway Map (30 分)

In the big cities, the subway systems always look so complex to the visitors. To give you some sense, the following figure shows the map of Beijing subway. Now you are supposed to help people with your computer skills! Given the starting position of your user, your task is to find the quickest way to his/her destination.

subwaymap.jpg

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤ 100), the number of subway lines. Then N lines follow, with the i-th (i=1,⋯,N) line describes the i-th subway line in the format:

M S[1] S[2] ... S[M]

where M (≤ 100) is the number of stops, and S[i]'s (i=1,⋯,M) are the indices of the stations (the indices are 4-digit numbers from 0000 to 9999) along the line. It is guaranteed that the stations are given in the correct order -- that is, the train travels between S[i] and S[i+1] (i=1,⋯,M−1) without any stop.

Note: It is possible to have loops, but not self-loop (no train starts from S and stops at S without passing through another station). Each station interval belongs to a unique subway line. Although the lines may cross each other at some stations (so called "transfer stations"), no station can be the conjunction of more than 5 lines.

After the description of the subway, another positive integer K (≤ 10) is given. Then K lines follow, each gives a query from your user: the two indices as the starting station and the destination, respectively.

The following figure shows the sample map.

samplemap.jpg

Note: It is guaranteed that all the stations are reachable, and all the queries consist of legal station numbers.

Output Specification:

For each query, first print in a line the minimum number of stops. Then you are supposed to show the optimal path in a friendly format as the following:

Take Line#X1 from S1 to S2.
Take Line#X2 from S2 to S3.
......

where Xi's are the line numbers and Si's are the station indices. Note: Besides the starting and ending stations, only the transfer stations shall be printed.

If the quickest path is not unique, output the one with the minimum number of transfers, which is guaranteed to be unique.

Sample Input:

4
7 1001 3212 1003 1204 1005 1306 7797
9 9988 2333 1204 2006 2005 2004 2003 2302 2001
13 3011 3812 3013 3001 1306 3003 2333 3066 3212 3008 2302 3010 3011
4 6666 8432 4011 1306
3
3011 3013
6666 2001
2004 3001

Sample Output:

2
Take Line#3 from 3011 to 3013.
10
Take Line#4 from 6666 to 1306.
Take Line#3 from 1306 to 2302.
Take Line#2 from 2302 to 2001.
6
Take Line#2 from 2004 to 1204.
Take Line#1 from 1204 to 1306.
Take Line#3 from 1306 to 3001.

 

最短路 變形, 不止一個屬性值,還有一個 換乘站的 屬性值。  距離第一,換乘站第二。

比較函式寫錯了,心態崩了。

#include<bits/stdc++.h>
using namespace std;

#define rep(i,a,b) for(int i=a;i<b;++i)
#define bug(x) printf("***x:%d\n",x);

const int N=1e4+10;
const int INF=0x3f3f3f3f;

struct Edge {
	int u,v,w,nt,ll;
	Edge(int _u=0,int _v=0,int _w=0,int _nt=0,int _ll=0) {
		u=_u,v=_v,w=_w,nt=_nt,ll=_ll;
	}
}edge[N*2];
int head[N],tot=0;
void add_edge(int u,int v,int ll) {
	edge[tot]=Edge(u,v,1,head[u],ll);
	head[u]=tot++;
}

struct Node {
	int x,d,y;
	Node(int _x=0,int _d=0,int _y=0) {
		x=_x,d=_d,y=_y;
	}
	bool operator<(const Node& b)const {
		if(d==b.d){
			if(y!=b.y)return y>b.y;
			return x>b.x;
		}
		return d>b.d;
	}
	bool operator==(const Node& b)const{
		return x==b.x&&d==b.d&&y==b.y;
	}
};

int path[N],tim[N],pre[N];
void get_path(int st,int x,int& tt){
	//printf("x:%d\n",x);
	if(x==st) {
		path[tt++]=x;
		return;
	}
	//printf("**cnt:%d\n",tt)
	get_path(st,pre[x],tt);
	path[tt++]=x;
}

void init() {
	rep(i,0,N)head[i]=-1;
}

int dis[N],pl[N];
int n;
int line[110][110];
int vis[N],arr[N],ID[N],cnt=0;
priority_queue<Node> pq;
int que[N],tran[N];
//set<pair<int,int> >has;
set<Node> has[N];
void diji(int st,int ed) {
	rep(i,1,cnt+1){
		dis[i]=INF,tran[i]=0,pl[i]=-1,pre[i]=-1;
		has[i].clear();
	}
	dis[st]=0;
	tran[st]=tim[st]>1?1:0;
	pq.push(Node(st,dis[st],tran[st]));
//	has.insert(Node(st,dis[st],tran[st]));
	while(!pq.empty()) {
		Node now=pq.top();
		pq.pop();
		if(has[now.d].count(now)!=0) continue;
		has[now.d].insert(now);

		for(int i=head[now.x]; i!=-1; i=edge[i].nt) {
			Edge& e=edge[i];

			int t=0;
			if(tim[e.v]>1)t=1;
			if(dis[e.v]>dis[e.u]+1) {
				dis[e.v]=dis[e.u]+1;
				pl[e.v]=e.ll;
				pre[e.v]=e.u;
				tran[e.v]=tran[e.u]+t;
				pq.push(Node(e.v,dis[e.v],tran[e.v]));
			}else if(dis[e.v]==dis[e.u]+1&&tran[e.u]+t<tran[e.v]){
				pl[e.v]=e.ll;
				pre[e.v]=e.u;
				tran[e.v]=tran[e.u]+t;
				pq.push(Node(e.v,dis[e.v],tran[e.v]));
			}
		//	printf("u:%d v:%d dis:%d %d\n",arr[e.u],arr[e.v],dis[e.v],tran[e.v]);
		}
	}
	//printf("****\n");
//	rep(i,0,cnt)printf("i:%d dis:%d\n",i,dis[i]);
	int tt=0;
	get_path(st,ed,tt);
	//path[cnt-1]=ed;
	//printf("st:%d ed:%d\n",st,ed);
	//printf("**dis:%d res:%d cnt:%d\n",dis[ed],res,tt);
	//rep(i,0,cnt)printf("**i:%d %d\n",i,pl[i]);
	if(tt==1)pl[path[0]]=que[path[0]];
	else pl[path[0]]=pl[path[1]];

	path[tt]=10010;
	pl[path[tt]]=10010;
	int t=1;
	printf("%d\n",dis[ed]);
	while(t<=tt) {
		int x=path[t];
		if(pl[x]!=pl[path[t-1]]) {
			printf("Take Line#%d from %04d to %04d.\n",pl[path[t-1]],arr[st],arr[path[t-1]]);
			st=path[t-1];
		}
		t++;
	}
}
/*
1
13 3011 3812 3013 3001 1306 3003 2333 3066 3212 3008 2302 3010 3011

100

1
2 1 2
100
*/
int main() {
	init();
	cnt=0;
	scanf("%d",&n);
	rep(i,1,n+1) {
		int m;
		scanf("%d",&m);
		line[i][0]=m;
		rep(j,1,m+1) {
			scanf("%d",&line[i][j]);
			arr[++cnt]=line[i][j];
		}
	}

	sort(arr+1,arr+cnt+1);
	cnt=unique(arr+1,arr+cnt+1)-arr;
	rep(i,1,cnt+1)ID[arr[i]]=i;

	rep(i,1,n+1) {
		rep(j,1,cnt+1)vis[j]=0;
		rep(j,1,line[i][0]+1) {
			line[i][j]=ID[line[i][j]];
			if(!vis[line[i][j]]) {
				vis[line[i][j]]=1;
				tim[line[i][j]]++;
			}

			if(j>1){
				add_edge(line[i][j-1],line[i][j],i);
				add_edge(line[i][j],line[i][j-1],i);
			}
			que[line[i][j]]=i;
		}
	}
	//
//	rep(i,1,cnt)printf("*i:%d %d  %d\n",i,arr[i],tim[i]);
	int T;
	scanf("%d",&T);
	rep(kase,0,T) {
		int u,v;
		scanf("%d %d",&u,&v);
		int x=ID[u],y=ID[v];
		diji(x,y);
	}
	return 0;
}