0
点赞
收藏
分享

微信扫一扫

HDU 2722 Here We Go(relians) Again (spfa(结构体)+建图)

倚然君 2023-02-07 阅读 139


Here WeGo(relians) Again

TimeLimit: 2000/1000 MS (Java/Others)    Memory Limit:32768/32768 K (Java/Others)
Total Submission(s): 1427    Accepted Submission(s): 712

ProblemDescription

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

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

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

The picture above shows a Gorelian City marked with speed limits, one waystreets, and one closed street. It is assumed that streets are always traveledat 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 routefrom 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 twoway traffic at 9 rels per blip, the fastest route becomes 1295 blips. On theother hand, suppose the three one-way streets are switched from southbound tonorthbound (with the closed road remaining closed). In that case, no routewould be possible and the day would be declared a holiday.

 

 

Input

The input consists of a set of cities forwhich you must find a fastest route if one exists. The first line of an inputcase contains two integers, which are the vertical and horizontal number ofcity blocks, respectively. The smallest city is a single block, or 1 by 1, andthe largest city is 20 by 20 blocks. The remainder of the input specifies speedlimits and traffic directions for streets between intersections, one row ofstreet segments at a time. The first line of the input (after the dimensionsline) contains the data for the northernmost east-west street segments. Thenext line contains the data for the northernmost row of north-south streetsegments. Then the next row of east-west streets, then north-south streets, andso on, until the southernmost row of east-west streets. Speed limits anddirections of travel are specified in order from west to east, and eachconsists of an integer from 0 to 9 indicating speed limit, and a symbolindicating which direction traffic may flow. A zero speed limit means the roadis closed. All digits and symbols are delimited by a single space. Foreast-west streets, the symbol will be an asterisk '*' which indicates travel isallowed in both directions, a less-than symbol '<' which indicates travel isallowed only in an east-to-west direction, or a greater-than symbol '>'which indicates travel is allowed only in a west-to-east direction. Fornorth-south streets, an asterisk again indicates travel is allowed in eitherdirection, a lowercase "vee" character 'v' indicates travel isallowed only in a north-to-south directions, and a caret symbol '^' indicatestravel is allowed only in a south-to-north direction. A zero speed, indicatinga closed road, is always followed by an asterisk. Input cities continue in thismanner until a value of zero is specified for both the vertical and horizontaldimensions.

 

 

Output

For each input scenario, output a linespecifying the integer number of blips of the shortest route, a space, and thenthe word "blips". For scenarios which have no route, output a linewith the word "Holiday".

 

 

SampleInput

2 2

9 * 9 *

6 v 0 *8 v

3 * 7 *

3 * 6 v3 *

4 * 8 *

2 2

9 * 9 *

6 v 9 *8 v

3 * 7 *

3 * 6 v3 *

4 * 8 *

2 2

9 * 9 *

6 ^ 0 *8 ^

3 * 7 *

3 * 6 ^3 *

4 * 8 *

0 0

 

 

SampleOutput

1715blips

1295blips

Holiday

 

 

Source

​​Mid-CentralUSA 2007 ​​

 

 

Recommend

teddy

  算法实现:

题目描述:

给出一个n*m的矩阵,起点在矩形的左上角,终点在右下角,里面一个小矩形代表一个街区(block)。每个小矩形的边长都是2520,小矩形的边有一个速度限制,范围是0~9,如果是0表示这条边不能行驶。关于输入方面(此题最恶心的地方),由上到下,从左到右,按照上图的对应的位置方式给出数据,  每一条边是  "数字"+“空格”+“符号”的形式,数字表示这条边的限速,符号表示这条路是单向(还分东西,南北)的还是双向的

‘*’:表示双向

‘>’:表示只能从左向右

‘<’:表示只能从右向左

‘^’:表示只能从下往上

‘v’:表示只能从上往下

如果一条路上的规定速度为0,表示不通

如果不能从左上到右下则输出“Holiday”

否则输出最短的时间

思路:建图加spfa算法

此题恶心的地方建图:就是把上图转为:

1    2   3

4    5   6

7    8   9

并且把路径限速和方向附上,看代码实现:

 

代码实现:


#include<bits/stdc++.h>
using namespace std;;
#define N 22
#define INF 0x7fffffff
struct node
{
int num,v;//num代表点数(序号),v为最大限速
node(int a,int b)
{
num=a;
v=b;
}
};
int n,m;
vector<node> g[N*N];
int vis[N*N],dis[N*N];
void spfa()
{
for(int i=1;i<=(n+1)*(m+1);i++)
dis[i]=INF,vis[i]=0;
queue<int> Q;
dis[1]=0;
vis[1]=1;
Q.push(1);
while(!Q.empty())
{
int u=Q.front();
Q.pop();
vis[u]=0;
for(int i=0;i<g[u].size();i++)
{
if(dis[g[u][i].num]<=dis[u]+2520/g[u][i].v) continue;
dis[g[u][i].num]=dis[u]+2520/g[u][i].v;
if(!vis[g[u][i].num])
{
vis[g[u][i].num]=1;
Q.push(g[u][i].num);
}
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0&&m==0)
break;
for(int i=1;i<=(n+1)*(m+1);i++)
g[i].clear();
for(int i=0;i<=n;i++) //从0开始
{

int x;
char s[5];
for(int j=1;j<=m;j++)//横向
{
scanf("%d%s",&x,s);
if(x==0) continue;

int p=i*(m+1)+j; //起始序号与n和m的关系(画图规律题)
int q=i*(m+1)+j+1; //终点序号与n和m的关系(画图规律题)

if(s[0]=='*') //双向
{
g[p].push_back(node(q,x));
g[q].push_back(node(p,x));
}
if(s[0]=='<') g[q].push_back(node(p,x));
if(s[0]=='>') g[p].push_back(node(q,x));

}
if(i==n) continue;
for(int j=1;j<=m+1;j++)//纵向多一个
{
scanf("%d%s",&x,s);
if(x==0) continue;
int p=i*(m+1)+j;
int q=(i+1)*(m+1)+j;

if(s[0]=='*')
{
g[p].push_back(node(q,x));
g[q].push_back(node(p,x));
}
if(s[0]=='^') g[q].push_back(node(p,x));
if(s[0]=='v') g[p].push_back(node(q,x));
}
}
spfa();
if(dis[(n+1)*(m+1)]==INF)
printf("Holiday\n");
else
printf("%d blips\n",dis[(n+1)*(m+1)]);
}
return 0;
}


举报

相关推荐

0 条评论