Google中國2014校園招聘筆試 Campus Test Problem E. Spaceship Defence

來源:互聯網
上載者:User

teleporters andturbolifts.

     to room  , the same turbolift cannot move soldiers from room  to room , although there might be another turbolift that does that.

T. T test cases follow.

N, which is the number of rooms in your spaceship. The rooms are numbered from 1 to N. The following N lines each contain a string telling the color of the rooms, from room 1 to room N. The strings only contain characters  (the lower-case English letters) and  (the number 0 to 9), and the length of each string will be less than or equal to 2.

M, which indicates the number of turbolifts in your spaceship. The following M lines each contain 3 space-separated integers aibiti, telling us that there is a turbolift that can transport soldiers from room ai to room bi in tiseconds.

S, which is the number of soldiers at your command. The following S lines each contain two integers: the location and destination of one soldier, pj and qj.

S lines, output a single integer: on line j, the smallest number of seconds it could take for a soldier to travel from pj to qj. If there is no path from pj to qj, the integer you output should be -1.

S ≤ 100.
1 ≤ ai, bi ≤ N.
0 ≤ ti ≤ 1000.
1 ≤ pj, qj ≤ N.

T ≤ 10.
1 ≤ N ≤ 1000.
0 ≤ M ≤ 3000.

T = 1.
1 ≤ N ≤ 80000.
0 ≤ M ≤ 3000.

 
 
 
 

類型:圖論  難度:2

題意:有編號1-n的n個點,給出每個點的顏色,同顏色的點可以不花時間立即到達,給出每條邊的起點終點和路徑長度,求給出的起點和終點之間的最短距離,若到達不了輸出-1。

分析:求單源最短距離的變型,將每個顏色看成一個點,將給出的邊化成每種顏色之間的邊,再將所求的起點和終點轉化為顏色用dij求最短距離即可。注意dij細節即可。

代碼:

#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<cstring>#include<string>#include<map>#include<set>#include<iostream>#define MIN(x,y) (x)<(y)?(x):(y)using namespace std;const int N=80100;const int M=1500;map<string,int> color;int colnum[N];int edge[M][M][2];int ednum[M][M];int cal(int st,int en){if(st == en)return 0;if(edge[st][en][1] == 1)return edge[st][en][0];map<int,int> minedge;edge[st][st][1] = 1;for(int i=1; i<=ednum[st][0]; i++){int pvt = ednum[st][i];minedge[pvt] = edge[st][pvt][0];}while(!minedge.empty()){map<int,int>::iterator it;int minnum = -1;for(it=minedge.begin(); it!=minedge.end(); it++){if(minnum<0 || it->second<minedge[minnum]){minnum = it->first;}}edge[st][minnum][1] = 1;for(int i=1; i<=ednum[minnum][0]; i++){int pvt = ednum[minnum][i];if(edge[st][pvt][0] == -1 || edge[st][pvt][0] > edge[st][minnum][0]+edge[minnum][pvt][0]){edge[st][pvt][0] = edge[st][minnum][0]+edge[minnum][pvt][0];minedge[pvt] = edge[st][pvt][0];}}minedge.erase(minnum);}for(int i=0; i<M; i++){edge[st][i][1] = 1;}return edge[st][en][0];}int main(){freopen("E-large-practice.in","r",stdin);freopen("E-large-practice.out","w",stdout);int t;scanf("%d",&t);for(int cnt=1;cnt<=t;cnt++){color.clear();memset(colnum,0,sizeof(colnum));memset(edge,-1,sizeof(edge));memset(ednum,0,sizeof(ednum));int n;scanf("%d",&n);int coln = 0;for(int i=1; i<=n; i++){char tmp[5];scanf("%s",tmp);string tmp_s = tmp;if(color[tmp_s] == 0)color[tmp_s] = ++coln;colnum[i] = color[tmp_s];//printf("dddd %d %d\n",i,colnum[i]);}int m;scanf("%d",&m);for(int i=0;i<m;i++){int st,en,ti;scanf("%d%d%d",&st,&en,&ti);int stc = colnum[st];int enc = colnum[en];if(stc == enc)continue;if(edge[stc][enc][0] == -1){edge[stc][enc][0] = ti;ednum[stc][++ednum[stc][0]] = enc;}else{if(edge[stc][enc][0] > ti)edge[stc][enc][0] = ti;}}/*printf("\nedge:\n");for(int i=1; i<=coln; i++){for(int j=1; j<=coln;j++)printf("%d ",edge[i][j][0]);printf("\n");}printf("\nedgenum:\n");for(int i=1; i<=coln; i++){printf("i:",i);for(int j=1; j<=ednum[i][0];j++)printf("%d ",ednum[i][j]);printf("\n");}*/printf("Case #%d:\n",cnt);int s;scanf("%d",&s);for(int i=0; i<s; i++){int st,en;scanf("%d%d",&st,&en);int stc = colnum[st];int enc = colnum[en];//printf("xxx %d %d\n",stc,enc);printf("%d\n",cal(stc,enc));}} } 


 

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.