2014
02-14

Here We Go(relians) Again

The Gorelians are a warlike race that travel the universe conquering new worlds as a form of recreation. Given their violent, fun-loving nature, keeping their leaders alive is of serious concern. Part of the Gorelian security plan involves changing the traffic patterns of their cities on a daily basis, and routing all Gorelian Government Officials to the Government Building by the fastest possible route.

Fortunately for the Gorelian Minister of Traffic (that would be you), all Gorelian cities are laid out as a rectangular grid of blocks, where each block is a square measuring 2520 rels per side (a rel is the Gorelian Official Unit of Distance). The speed limit between two adjacent intersections is always constant, and may range from 1 to 9 rels per blip (a blip, of course, being the Gorelian Official Unit of Time). Since Gorelians have outlawed decimal numbers as unholy (hey, if you’re the dominant force in the known universe, you can outlaw whatever you want), speed limits are always integer values. This explains why Gorelian blocks are precisely 2520 rels in length: 2520 is the least common multiple of the integers 1 through 9. Thus, the time required to travel between two adjacent intersections is always an integer number of blips.

In all Gorelian cities, Government Housing is always at the northwest corner of the city, while the Government Building is always at the southeast corner. Streets between intersections might be one-way or two-way, or possibly even closed for repair (all this tinkering with traffic patterns causes a lot of accidents). Your job, given the details of speed limits, street directions, and street closures for a Gorelian city, is to determine the fastest route from Government Housing to the Government Building. (It is possible, due to street directions and closures, that no route exists, in which case a Gorelian Official Temporary Holiday is declared, and the Gorelian Officials take the day off.)

The picture above shows a Gorelian City marked with speed limits, one way streets, and one closed street. It is assumed that streets are always traveled at the exact posted speed limit, and that turning a corner takes zero time. Under these conditions, you should be able to determine that the fastest route from Government Housing to the Government Building in this city is 1715 blips. And if the next day, the only change is that the closed road is opened to two way traffic at 9 rels per blip, the fastest route becomes 1295 blips. On the other hand, suppose the three one-way streets are switched from southbound to northbound (with the closed road remaining closed). In that case, no route would be possible and the day would be declared a holiday.

The input consists of a set of cities for which you must find a fastest route if one exists. The first line of an input case contains two integers, which are the vertical and horizontal number of city blocks, respectively. The smallest city is a single block, or 1 by 1, and the largest city is 20 by 20 blocks. The remainder of the input specifies speed limits and traffic directions for streets between intersections, one row of street segments at a time. The first line of the input (after the dimensions line) contains the data for the northernmost east-west street segments. The next line contains the data for the northernmost row of north-south street segments. Then the next row of east-west streets, then north-south streets, and so on, until the southernmost row of east-west streets. Speed limits and directions of travel are specified in order from west to east, and each consists of an integer from 0 to 9 indicating speed limit, and a symbol indicating which direction traffic may flow. A zero speed limit means the road is closed. All digits and symbols are delimited by a single space. For east-west streets, the symbol will be an asterisk ‘*’ which indicates travel is allowed in both directions, a less-than symbol ‘<’ which indicates travel is allowed only in an east-to-west direction, or a greater-than symbol ‘>’ which indicates travel is allowed only in a west-to-east direction. For north-south streets, an asterisk again indicates travel is allowed in either direction, a lowercase "vee" character ‘v’ indicates travel is allowed only in a north-to-south directions, and a caret symbol ‘^’ indicates travel is allowed only in a south-to-north direction. A zero speed, indicating a closed road, is always followed by an asterisk. Input cities continue in this manner until a value of zero is specified for both the vertical and horizontal dimensions.

The input consists of a set of cities for which you must find a fastest route if one exists. The first line of an input case contains two integers, which are the vertical and horizontal number of city blocks, respectively. The smallest city is a single block, or 1 by 1, and the largest city is 20 by 20 blocks. The remainder of the input specifies speed limits and traffic directions for streets between intersections, one row of street segments at a time. The first line of the input (after the dimensions line) contains the data for the northernmost east-west street segments. The next line contains the data for the northernmost row of north-south street segments. Then the next row of east-west streets, then north-south streets, and so on, until the southernmost row of east-west streets. Speed limits and directions of travel are specified in order from west to east, and each consists of an integer from 0 to 9 indicating speed limit, and a symbol indicating which direction traffic may flow. A zero speed limit means the road is closed. All digits and symbols are delimited by a single space. For east-west streets, the symbol will be an asterisk ‘*’ which indicates travel is allowed in both directions, a less-than symbol ‘<’ which indicates travel is allowed only in an east-to-west direction, or a greater-than symbol ‘>’ which indicates travel is allowed only in a west-to-east direction. For north-south streets, an asterisk again indicates travel is allowed in either direction, a lowercase "vee" character ‘v’ indicates travel is allowed only in a north-to-south directions, and a caret symbol ‘^’ indicates travel is allowed only in a south-to-north direction. A zero speed, indicating a closed road, is always followed by an asterisk. Input cities continue in this manner until a value of zero is specified for both the vertical and horizontal dimensions.

2 2
9 * 9 *
6 v 0 * 8 v
3 * 7 *
3 * 6 v 3 *
4 * 8 *
2 2
9 * 9 *
6 v 9 * 8 v
3 * 7 *
3 * 6 v 3 *
4 * 8 *
2 2
9 * 9 *
6 ^ 0 * 8 ^
3 * 7 *
3 * 6 ^ 3 *
4 * 8 *
0 0

1715 blips
1295 blips
Holiday

/*

卓提不系老难啊，主要系咋弄那与哦表，子要系能弄

俺用类SPFA，弄类系邻接表。
1a~

2012-07-25
*/

#include"stdio.h"
#include"string.h"

struct A
{
int total;
int dis;
int mem[6];
int len[6];
}E[444];

int MIN(int a,int b)
{
return a>b?b:a;
}

void SPFA(int s)
{
int k,key;
int hash[555];
int i;
int queue[888];

queue[0]=s;
key=1;
k=0;
memset(hash,0,sizeof(hash));
hash[s]=1;
while(k<key)
{
for(i=0;i<E[queue[k]].total;i++)
{
if(E[queue[k]].dis+E[queue[k]].len[i]<E[E[queue[k]].mem[i]].dis)
{
E[E[queue[k]].mem[i]].dis=E[queue[k]].dis+E[queue[k]].len[i];
if(!hash[E[queue[k]].mem[i]])
{
hash[E[queue[k]].mem[i]]=1;
queue[key++]=E[queue[k]].mem[i];
}
}
}
hash[queue[k++]]=0;
}
}
int main()
{
int n,m;
int n_temp;
int i,l;
int limit;
int temp;
int t1,t2;
char str[10];

while(scanf("%d%d",&n,&m),n||m)
{
temp=(n+1)*(m+1);
for(i=1;i<=temp;i++)	{E[i].total=0;E[i].dis=11111111;}

n_temp=2*n+1;
for(i=0;i<n_temp;i++)
{
if(i%2)	limit=m+1;
else	limit=m;
for(l=0;l<limit;l++)
{
scanf("%d%s",&temp,str);
if(temp==0)	continue;
temp=2520/temp;
if(i%2==0)	{t1=(i/2)*(m+1)+l+1;t2=t1+1;}
else		{t1=((i-1)/2)*(m+1)+l+1;t2=t1+m+1;}
if(str[0]=='*')
{
E[t1].mem[E[t1].total]=t2;
E[t1].len[E[t1].total++]=temp;
E[t2].mem[E[t2].total]=t1;
E[t2].len[E[t2].total++]=temp;
}
else if(str[0]=='v')
{
E[t1].mem[E[t1].total]=t2;
E[t1].len[E[t1].total++]=temp;
}
else if(str[0]=='^')
{
E[t2].mem[E[t2].total]=t1;
E[t2].len[E[t2].total++]=temp;
}
else if(str[0]=='>')
{
E[t1].mem[E[t1].total]=t2;
E[t1].len[E[t1].total++]=temp;
}
else if(str[0]=='<')
{
E[t2].mem[E[t2].total]=t1;
E[t2].len[E[t2].total++]=temp;
}
}
}

E[1].dis=0;
SPFA(1);

if(E[(n+1)*(m+1)].dis==11111111)	printf("Holiday\n");
else								printf("%d blips\n",E[(n+1)*(m+1)].dis);
}
return 0;
}

1. 这道题目虽然简单，但是小编做的很到位，应该会给很多人启发吧！对于面试当中不给开辟额外空间的问题不是绝对的，实际上至少是允许少数变量存在的。之前遇到相似的问题也是恍然大悟，今天看到小编这篇文章相见恨晚。