周遊世界是件浪漫事,但規劃旅行路線就不必定了…… 全世界有成千上萬條航線、鐵路線、大巴線,使人眼花繚亂。因此旅行社會選擇部分運輸公司組成聯盟,每家公司提供一條線路,而後幫助客戶規劃由聯盟內企業支持的旅行路線。本題就要求你幫旅行社實現一個自動規劃路線的程序,使得對任何給定的起點和終點,能夠找出最順暢的路線。所謂「最順暢」,首先是指中途經停站最少;若是經停站同樣多,則取須要換乘線路次數最少的路線。ios
輸入格式:
輸入在第一行給出一個正整數N(≤),即聯盟公司的數量。接下來有N行,第i行(,)描述了第i家公司所提供的線路。格式爲:測試
M S[1] S[2] ⋯ S[M]spa
其中M(≤)是經停站的數量,S[i](,)是經停站的編號(由4位0-9的數字組成)。這裏假設每條線路都是簡單的一條能夠雙向運行的鏈路,而且輸入保證是按照正確的經停順序給出的 —— 也就是說,任意一對相鄰的S[i]和S[i+1](,)之間都不存在其餘經停站點。咱們稱相鄰站點之間的線路爲一個運營區間,每一個運營區間只承包給一家公司。環線是有可能存在的,但不會不經停任何中間站點就從出發地回到出發地。固然,不一樣公司的線路是可能在某些站點有交叉的,這些站點就是客戶的換乘點,咱們假設任意換乘點涉及的不一樣公司的線路都不超過5條。code
在描述了聯盟線路以後,題目將給出一個正整數K(≤),隨後K行,每行給出一位客戶的需求,即始發地的編號和目的地的編號,中間以一空格分隔。blog
輸出格式:
處理每一位客戶的需求。若是沒有現成的線路可使其到達目的地,就在一行中輸出「Sorry, no line is available.」;若是目的地可達,則首先在一行中輸出最順暢路線的經停站數量(始發地和目的地不包括在內),而後按下列格式給出旅行路線:string
Go by the line of company #X1 from S1 to S2. Go by the line of company #X2 from S2 to S3. ......
其中Xi
是線路承包公司的編號,Si
是經停站的編號。但必須只輸出始發地、換乘點和目的地,不能輸出中間的經停站。題目保證知足要求的路線是惟一的。it
輸入樣例:
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.
#include <iostream> #include <cstdio> #include <vector> #include <cstdlib> #define inf 0x3f3f3f3f using namespace std; typedef pair<int,int> pa; int n,m,k; vector<pa> z[10000];///鄰接表記錄鄰接點以及屬於第幾家公司 int l[10000],path[10000];///l記錄某個站屬於第幾家公司 若是是中轉站值爲-1 path記錄dfs過程當中的站點 bool vis[10000]; int ans[10000],cnt,lnum; vector<int> s; int line(int a,int b) {///返回a->b屬於第幾家公司的路線 for(int i = 0;i < z[a].size();i ++) { if(z[a][i].first == b) return z[a][i].second; } } void dfs(int st,int en,int snum,int tnum,int lastline) { path[snum] = st; if(st == en) { if(snum < cnt || snum == cnt && tnum < lnum) { cnt = snum; lnum = tnum; for(int i = 0;i <= cnt;i ++) { ans[i] = path[i]; } } return; } for(int i = 0;i < z[st].size();i ++) { int d = z[st][i].first; if(vis[d]) continue; vis[d] = true; if(l[st] == -1) { int e = line(path[snum],d); if(e != lastline) { dfs(d,en,snum + 1,tnum + 1,e); } else dfs(d,en,snum + 1,tnum,lastline); } else dfs(d,en,snum + 1,tnum,lastline); vis[d] = false; } } int main() { scanf("%d",&n); for(int i = 0;i < n;i ++) { scanf("%d",&m); int a = -1,b; scanf("%d",&a); if(l[a]) l[a] = -1; else l[a] = i + 1; if(z[a].empty()) s.push_back(a); for(int j = 1;j < m;j ++) { scanf("%d",&b); if(l[b]) l[b] = -1; else l[b] = i + 1; if(z[b].empty()) s.push_back(b); z[a].push_back(pa(b,i + 1)); z[b].push_back(pa(a,i + 1)); a = b; } } scanf("%d",&k); while(k --) { int a,b; cnt = inf; lnum = inf; scanf("%d%d",&a,&b); dfs(a,b,0,0,0); if(cnt == inf) { printf("Sorry, no line is available.\n"); } else { printf("%d\n",cnt); for(int i = 1;i <= cnt;i ++) { if(i == cnt || l[ans[i]] == -1 && line(ans[i - 1],ans[i]) != line(ans[i],ans[i + 1])) { printf("Go by the line of company #%d from %04d to %04d.\n",line(ans[i - 1],ans[i]),a,ans[i]); a = ans[i]; } } } } }
再次作,原本只想記錄每一個站點都屬於哪些公司,可是有個測試點不對,兩個站點即便相鄰,可能也會同時屬於不一樣的公司。。應該是這樣,記錄兩個相鄰站點屬於的公司就對了。io
代碼:ast
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <algorithm> #define inf 0x3f3f3f3f using namespace std; typedef pair<int,int> pa; int n,m; vector<pa> mp[10000]; vector<int> ans,path,lans,line; bool vis[10000]; int min_,tmin_; int check(int a,int b) { for(int i = 0;i < mp[a].size();i ++) { if(mp[a][i].first == b) return mp[a][i].second; } } void dfs(int now,int en,int num,int tnum,int last) { if(now == en) { if(num < min_ || num == min_ && tnum < tmin_) { min_ = num; tmin_ = tnum; ans = path; lans = line; } return; } for(int i = 0;i < mp[now].size();i ++) { if(vis[mp[now][i].first]) continue; int d = check(now,mp[now][i].first); vis[mp[now][i].first] = true; if(last != d) { line.push_back(d); path.push_back(now); dfs(mp[now][i].first,en,num + 1,tnum + 1,d); path.pop_back(); line.pop_back(); } else dfs(mp[now][i].first,en,num + 1,tnum,last); vis[mp[now][i].first] = false; } } int main() { int a,b,k; scanf("%d",&n); for(int i = 0;i < n;i ++) { scanf("%d",&m); scanf("%d",&a); for(int j = 1;j < m;j ++) { scanf("%d",&b); mp[a].push_back(pa(b,i + 1)); mp[b].push_back(pa(a,i + 1)); a = b; } } scanf("%d",&k); for(int i = 0;i < k;i ++) { tmin_ = min_ = inf; path.clear(); line.clear(); scanf("%d%d",&a,&b); vis[a] = true; dfs(a,b,0,0,0); vis[a] = false; if(min_ == inf) { printf("Sorry, no line is available.\n"); } else { printf("%d\n",min_); if(a != b) ans.push_back(b); for(int j = 1;j < ans.size();j ++) { printf("Go by the line of company #%d from %04d to %04d.\n",lans[j - 1],ans[j - 1],ans[j]); } } } return 0; }