L3-014. 周遊世界
周遊世界是件浪漫事,但規劃旅行路線就不一定了…… 全世界有成千上萬條航線、鐵路線、大巴線,令人眼花繚亂。所以旅行社會選擇部分運輸公司組成聯盟,每家公司提供一條線路,然後幫助客戶規劃由聯盟內企業支持的旅行路線。本題就要求你幫旅行社實現一個自動規劃路線的程序,使得對任何給定的起點和終點,可以找出最順暢的路線。所謂“最順暢”,首先是指中途經停站最少;如果經停站一樣多,則取需要換乘線路次數最少的路線。
輸入格式:
輸入在第一行給出一個正整數N(<= 100),即聯盟公司的數量。接下來有N行,第i行(i=1, ..., N)描述了第i家公司所提供的線路。格式爲:
M S[1] S[2] ... S[M]
其中M(<= 100)是經停站的數量,S[i](i=1, ..., M)是經停站的編號(由4位0-9的數字組成)。這裏假設每條線路都是簡單的一條可以雙向運行的鏈路,並且輸入保證是按照正確的經停順序給出的 —— 也就是說,任意一對相鄰的S[i]和S[i+1](i=1, ..., M-1)之間都不存在其他經停站點。我們稱相鄰站點之間的線路爲一個運營區間,每個運營區間只承包給一家公司。環線是有可能存在的,但不會不經停任何中間站點就從出發地回到出發地。當然,不同公司的線路是可能在某些站點有交叉的,這些站點就是客戶的換乘點,我們假設任意換乘點涉及的不同公司的線路都不超過5條。
在描述了聯盟線路之後,題目將給出一個正整數K(<= 10),隨後K行,每行給出一位客戶的需求,即始發地的編號和目的地的編號,中間以一空格分隔。
輸出格式:
處理每一位客戶的需求。如果沒有現成的線路可以使其到達目的地,就在一行中輸出“Sorry, no line is available.”;如果目的地可達,則首先在一行中輸出最順暢路線的經停站數量(始發地和目的地不包括在內),然後按下列格式給出旅行路線:
Go by the line of company #X1 from S1 to S2. Go by the line of company #X2 from S2 to S3. ......
其中Xi是線路承包公司的編號,Si是經停站的編號。但必須只輸出始發地、換乘點和目的地,不能輸出中間的經停站。題目保證滿足要求的路線是唯一的。
輸入樣例: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 4 3011 3013 6666 2001 2004 3001 2222 6666輸出樣例:
2 Go by the line of company #3 from 3011 to 3013. 10 Go by the line of company #4 from 6666 to 1306. Go by the line of company #3 from 1306 to 2302. Go by the line of company #2 from 2302 to 2001. 6 Go by the line of company #2 from 2004 to 1204. Go by the line of company #1 from 1204 to 1306. Go by the line of company #3 from 1306 to 3001. Sorry, no line is available.
pat裏的最短路,一般都是條件很多的,又是路線,又是換乘的。這道題目用Dijkstra的時候應該注意,如果一般的Dijkstra以點爲節點插入優先隊列
那麼會有這樣的情況:到達同一個點的最優線路有兩條,那麼你就要再開一個數組記錄並列的最優線路有多少條。如果以邊爲節點插入優先隊列,那麼就不存在
多條最優路線的情況,
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <string>
#include <queue>
using namespace std;
const int maxn=1e5;
typedef long long int LL;
const int INF=1e9;
struct Node
{
int value;
int next;
int value2;
int id;
}edge[maxn*4+5],a[maxn+5];
int head[maxn+5];
int tot;
void add(int x,int y,int z)
{
edge[tot].value=y;
edge[tot].value2=x;
edge[tot].next=head[x];
edge[tot].id=z;
head[x]=tot++;
}
struct node
{
int pre;
int next;
int id;
int num;
int num2;
int pos;
node(){};
node(int pre,int next,int id,int num,int num2,int pos)
{
this->pre=pre;
this->next=next;
this->id=id;
this->num=num;
this->num2=num2;
this->pos=pos;
}
friend bool operator <(node a,node b)
{
return a.num>b.num;
}
};
int d[maxn+5];//站點數19958
int v[maxn+5];//換乘次數
int n,m;
int route[maxn+5];
int vis[maxn+5];
int Dijkstra(int s,int e)
{
priority_queue<node> q;
int res1=INF;
int res2=INF;
int ans=-1;
memset(vis,0,sizeof(vis));
for(int i=0;i<=tot;i++)
d[i]=v[i]=INF;
for(int i=head[s];i!=-1;i=edge[i].next)
{
d[i]=1;v[i]=0;
route[i]=-1;
vis[i]=1;
q.push(node(s,edge[i].value,edge[i].id,d[i],v[i],i));
}
while(!q.empty())
{
node term=q.top();
q.pop();
vis[term.pos]=1;
if(term.next==e)
{
if(res1>term.num)
{
ans=term.pos;
res1=term.num;
res2=term.num2;
}
else if(res1==term.num)
{
if(res2>term.num2)
{
res2=term.num2;
ans=term.pos;
}
}
}
for(int i=head[term.next];i!=-1;i=edge[i].next)
{
int x=edge[i].value;
if(vis[i]) continue;
if(d[i]>term.num+1)
{
d[i]=term.num+1;
if(term.id!=edge[i].id)
v[i]=term.num2+1;
else
v[i]=term.num2;
route[i]=term.pos;
q.push(node(term.next,edge[i].value,edge[i].id,d[i],v[i],i));
}
else if(d[i]==term.num+1)
{
int xx;
if(term.id!=edge[i].id)
xx=term.num2+1;
else
xx=term.num2;
if(v[i]>xx)
{
v[i]=xx;
route[i]=term.pos;
q.push(node(term.next,edge[i].value,edge[i].id,d[i],v[i],i));
}
}
}
}
return ans;
}
int main()
{
scanf("%d",&n);
int k;
memset(head,-1,sizeof(head));
tot=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&k);
int x,y=-1;
for(int j=1;j<=k;j++)
{
scanf("%d",&x);
if(y==-1)
{
y=x;
continue;
}
else
{
add(x,y,i);
add(y,x,i);
}
y=x;
}
}
scanf("%d",&m);
int x,y;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
//if(x==y)
//{printf("0\n");continue;}
int res=Dijkstra(x, y);
if(res==-1)
{printf("Sorry, no line is available.\n");continue;}
printf("%d\n",d[res]);
int cot=0;
while(res!=-1)
{
a[++cot]=edge[res];
res=route[res];
}
int p=-1;
int tag=0;
while(cot>=2)
{
if(a[cot].id!=p)
{
if(p!=-1)
printf(" to %04d.\n",a[cot].value2);
printf("Go by the line of company #%d from %04d",a[cot].id,a[cot].value2);
p=a[cot].id;
}
cot--;
tag=1;
}
if(a[cot].id!=p)
{
if(tag)
printf(" to %04d.\n",a[cot].value2);
printf("Go by the line of company #%d from %04d to %04d.\n",a[cot].id,a[cot].value2,a[cot].value);
}
else
printf(" to %04d.\n",a[cot].value);
}
return 0;
}