ACdream P1190 Spaceship Defence 缩点最短路

4 篇文章 0 订阅
1 篇文章 0 订阅

Spaceship Defence

Time Limit: 4000/2000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)
Problem Description

The enemy has invaded your spaceship, and only superior tactics will allow you to defend it! To travel around your spaceship, your soldiers will use two devices: teleporters and turbolifts.

Teleporters allow your soldiers to move instantly between rooms. Every room contains a teleporter, and rooms are color-coded: if a soldier is in a room with some color, she can use the teleporter in that room to immediately move to any other room with the same color.

Turbolifts allow your soldiers to move between rooms more slowly. A turbolift is like an elevator that moves in many directions. Each turbolift moves from one room to one other room, and it takes a certain amount of time to travel. Notes about turbolifts:

  • Turbolifts are not two-way: if a turbolift moves soldiers from room a to room b, the same turbolift cannot move soldiers from room b to room a, although there might be another turbolift that does that.
  • More than one soldier can use the same turbolift, and they do not interfere with each other in any way.

You will be given the locations and destinations of several soldiers. For each soldier, output the minimum amount of time it could take that soldier to travel from his location to his destination.

Input

The first line of the input gives the number of test cases, T(1 ≤ T ≤ 10)T test cases follow.

For every test case:

The first line of every test case contains an integer N(1 ≤ N ≤ 80000), 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 a-z (the lower-case English letters) and 0-9 (the number 0 to 9), and the length of each string will be less than or equal to 2.

The next line in the test case is an integer M(0 ≤ M ≤ 3000), which indicates the number of turbolifts in your spaceship. The following M lines each contain 3 space-separated integers ai, bi, ti(1 ≤ ai, bi ≤ N, 0 ≤ ti ≤ 1000), telling us that there is a turbolift that can transport soldiers from room ai to room bi in ti seconds.

The next line in the test case contains an integer S(1 ≤ S ≤ 100), which is the number of soldiers at your command. The followingS lines each contain two integers: the location and destination of one soldier, pj and qj(1 ≤ pj, qj ≤ N).

Output

For each test case, output one line containing only the string "Case #x:", where x is the number of the test case (starting from 1).

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

Sample Input
3
3
gl
t3
t3
3
1 2 217
3 2 567
1 1 21
2
2 1
2 3
4
ca
bl
bl
8z
0
3
1 2
2 3
1 1
8
re
b7
ye
gr
0l
0l
ye
b7
7
4 1 19
2 4 21
2 5 317
4 5 34
4 7 3
4 8 265
8 6 71
3
4 3
2 6
1 4
Sample Output
Case #1:
-1
0
Case #2:
-1
0
0
Case #3:
3
55
-1
Source
codejam
Manager

模板题目,做出这道题的关键在于你是否对自己的模板足够信任。本题主要有这么一些要点和对应算法:

1:缩点 构造MAP表轻松搞定

2:边用邻接表存储 邻接表的存储

3:最短路问题 SPFA等等

4:同一点不重复计算 排序可破


代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<string>
#include<cstring>
#include<algorithm>
#include<fstream>
#include<queue>
#include<stack> 
#include<vector>
#include<map>
#include<cmath>
#include<iomanip>
#define rep(i,n) for(i=1;i<=n;i++)
#define MM(a,t) memset(a,t,sizeof(a))
#define INF 1e9
typedef long long ll;
#define mod 1000000007
using namespace std;
struct node{
  char st[10];	
}a[80010];
struct edge{
  int e,w;	
}dui[3010];
struct query{
  int lo,de;
  int index;
}b[200];
int res[200],dist[80010];
map<string,int> co; 
int n,nn,m,S,fir[80010],lst[80010],nxt[3010];
bool cmp(query a1,query a2){
	return a1.lo<a2.lo;
}
queue<int> q;
bool vis[80010];
void SPFA(int sta){
	int i,j;
	
    while(!q.empty()) q.pop();
    MM(vis,0);
	rep(i,nn) dist[i]=-1;
    dist[sta]=0; q.push(sta);
	while(!q.empty()){
		int tmp=q.front(); q.pop(); vis[tmp]=false;
		for(i=fir[tmp];i!=-1;i=nxt[i]){
		  int e=dui[i].e,w=dui[i].w;
		  if(dist[e]==-1 || dist[tmp]+w<dist[e]){
	        dist[e]=dist[tmp]+w;
			if(!vis[e]){
				vis[e]=true;
				q.push(e);
			} 
  		  }
		}
	}
}
int main()
{
	int i,j,T,n2;
 
    scanf("%d",&T);
    rep(n2,T){
      scanf("%d",&n); co.clear(); nn=0;
	  rep(i,n){
  	    scanf("%s",a[i].st);
		if(co[a[i].st]==0) co[a[i].st]=++nn;	
  	  }	
      scanf("%d",&m); MM(fir,-1); MM(lst,-1); MM(nxt,-1); 
      rep(i,m){
      	int s,e,w;
      	scanf("%d%d%d",&s,&e,&w);
      	s=co[a[s].st]; e=co[a[e].st];
      	dui[i].e=e; dui[i].w=w;
      	if(fir[s]==-1){fir[s]=i;      lst[s]=i;}
      	else          {nxt[lst[s]]=i; lst[s]=i;}
      }
      scanf("%d",&S);
      rep(i,S){
        scanf("%d%d",&b[i].lo,&b[i].de);
        b[i].lo=co[a[b[i].lo].st]; b[i].de=co[a[b[i].de].st];
        b[i].index=i;
	  }
	  sort(b+1,b+S+1,cmp);
	  rep(i,S){
  	    int lo=b[i].lo,de=b[i].de,index=b[i].index;
		if(i==1 || lo!=b[i-1].lo){
		   SPFA(lo);	
		}	
		res[index]=dist[de];
  	  }
  	  printf("Case #%d:\n",n2);
  	  rep(i,S) cout<<res[i]<<'\n';
    }
    
	
	return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值