You are playing a game with your elder brother.
First, a number of circles and arrows connecting some pairs of the circles are drawn on the ground.
Two of the circles are marked as the start circle and the goal circle.
At the start of the game, you are on the start circle. In each turn of the game, your brother tells
you a number, and you have to take that number of steps. At each step, you choose one of the arrows
outgoing from the circle you are on, and move to the circle the arrow is heading to. You can visit the
same circle or use the same arrow any number of times.
Your aim is to stop on the goal circle after the fewest possible turns, while your brother’s aim is to
prevent it as long as possible. Note that, in each single turn, you must take the exact number of steps
your brother tells you. Even when you visit the goal circle during a turn, you have to leave it if more
steps are to be taken.
If you reach a circle with no outgoing arrows before completing all the steps, then you lose the
game. You also have to note that, your brother may be able to repeat turns forever, not allowing you
to stop after any of them.
Your brother, mean but not too selfish, thought that being allowed to choose arbitrary numbers
is not fair. So, he decided to declare three numbers at the start of the game and to use only those
numbers.
Your task now is, given the configuration of circles and arrows, and the three numbers declared, to
compute the smallest possible number of turns within which you can always finish the game, no matter
how your brother chooses the numbers.
Input
The input file contains several test cases, each of them as described below.
The input consists must be formatted as follows:
n m a b c
u1 v1
.
.
.
um vm
All numbers in a test case are integers. n is the number of circles (2 ≤ n ≤ 50). Circles are numbered
1 through n. The start and goal circles are numbered 1 and n, respectively. m is the number of arrows
(0 ≤ m ≤ n(n−1)). a, b, and c are the three numbers your brother declared (1 ≤ a, b, c ≤ 100). The
pair, ui and vi
, means that there is an arrow from the circle ui to the circle vi
. It is ensured that
ui ̸= vi for all i, and ui ̸= uj or vi ̸= vj if i ̸= j.
Output
For each test case, print the smallest possible number of turns within which you can always finish the
game on a line by itself.
Print ‘IMPOSSIBLE’ if your brother can prevent you from reaching the goal, by either making you
repeat the turns forever or leading you to a circle without outgoing arrows.
Explanations:
On the first case of Sample Input below, your brother may choose 1 first, then 2, and repeat these
forever. Then you can never finish.
On the second case (figure on the right), if your
brother chooses 2 or 3, you can finish with a single
turn. If he chooses 1, you will have three options.
• Move to the circle 5. This is a bad idea: Your
brother may then choose 2 or 3 and make you
lose.
• Move to the circle 4. This is the best choice:
From the circle 4, no matter any of 1, 2, or 3
your brother chooses in the next turn, you can
finish immediately.
• Move to the circle 2. This is not optimal for you.
If your brother chooses 1 in the next turn, you cannot finish yet. It will take three or more turns
in total.
In summary, no matter how your brother acts, you can finish within two turns. Thus the answer is
2.
Sample Input
3 3 1 2 4
1 2
2 3
3 1
8 12 1 2 3
1 2
2 3
1 4
2 4
3 4
1 5
5 8
4 6
6 7
4 8
6 8
7 8
Sample Output
IMPOSSIBLE

题意:给你n个点(n<=50),然后有些点之间会有一条路,路是单向的,每个回合让你走a,b,c三种步数中的任意一种(a,b,c<=100),问你最少需要多少个回合才能保证一定能从1点到达n点;

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <queue>
#include <vector>
#define MM(a,b) memset(a,b,sizeof(a));
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
#define CT continue
#define SC scanf
const int N=1e5+10;
int dis[55][7],step[7],vis[55][105];
int n,m,a,b,c;
vector<int> nxt[55][6],G[55]; void initdfs(int root,int u,int stepf,int d)
{
vis[u][d]=1;
if(d==0) {
nxt[u][stepf].push_back(root);
return;
}
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(!vis[v][d-1]) initdfs(root,v,stepf,d-1);
}
} void initstep()
{
for(int i=1;i<=n;i++)
for(int j=1;j<=3;j++) {
MM(vis,0);
initdfs(i,i,j,step[j]);
}
} void init()
{
for(int i=1;i<=n;i++) {
nxt[i][1].clear();
nxt[i][2].clear();
nxt[i][3].clear();
G[i].clear();
}
} struct node{
int v,dis4;
bool operator<(const node &a) const{
return this->dis4>a.dis4;
}
};
priority_queue<node> q; int dist_road(int s)
{
while(q.size()) q.pop();
MM(dis,inf);
MM(dis[s],0); q.push((node){s,0});
while(q.size()){
node cur=q.top();q.pop();
int u=cur.v;
if(dis[u][4]<cur.dis4) CT;
for(int i=1;i<=3;i++)
for(int j=0;j<nxt[u][i].size();j++){
int v=nxt[u][i][j];
if(dis[v][i]>dis[u][4]+1)
dis[v][i]=dis[u][4]+1;
if(dis[v][4]>max(max(dis[v][1],dis[v][2]),dis[v][3])){
dis[v][4]=max(max(dis[v][1],dis[v][2]),dis[v][3]);
q.push((node){v,dis[v][4]});
} }
}
return dis[1][4];
} int main()
{
while(~SC("%d%d%d%d%d",&n,&m,&step[1],&step[2],&step[3]))
{
init();
for(int i=1;i<=m;i++){
int u,v;
SC("%d%d",&u,&v);
G[u].push_back(v);
} initstep(); int k=dist_road(n);
if(k==inf) printf("IMPOSSIBLE\n");
else printf("%d\n",k);
}
return 0;
}

 分析:

1.比赛时有个很关键的地方没有分析出来那就是对于点n,如果答案有解,那么n向前操作一个回合后,

至少存在一个点,使得其走a,b,c三种步数都可以到达n,然后再拿这些点去更新其他的点,并且一定可以

更新到1号点

2.

void initdfs(int root,int u,int stepf,int d)
{
vis[u][d]=1;
if(d==0) {
nxt[u][stepf].push_back(root);
return;
}
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(!vis[v][d-1]) initdfs(root,v,stepf,d-1);
}
}
void initstep()
{
for(int i=1;i<=n;i++)
for(int j=1;j<=3;j++) {
MM(vis,0);
initdfs(i,i,j,step[j]);
}
}

  对于这段初始化每个点走a,b,c三种步数能够到达的点,刚开始没加vis[][]数组:考虑一个完全图(任意两点之间都有边相连接)的话,那么对于50个点,可以走100步,每步都可以走50个点,复杂度就是

50*100^50显然会超时,,因为进行了大量的重复计算,加个有效的优化,vis数组,vis[a][b],表示对于

走到a节点剩余步数为b步,,,那么复杂度就降为50*(50*100)

UVAlive 7414 Squeeze the Cylinders a,b,c三种步数 搜索+最短路的更多相关文章

  1. UVALive 6257 Chemist's vows --一道题的三种解法(模拟,DFS,DP)

    题意:给一个元素周期表的元素符号(114种),再给一个串,问这个串能否有这些元素符号组成(全为小写). 解法1:动态规划 定义:dp[i]表示到 i 这个字符为止,能否有元素周期表里的符号构成. 则有 ...

  2. What a Ridiculous Election UVALive - 7672 (BFS)

    题目链接: E - What a Ridiculous Election  UVALive - 7672 题目大意: 12345 可以经过若干次操作转换为其它五位数. 操作分三种,分别为: 操作1:交 ...

  3. POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Network / FZU 1161 (网络流,最大流)

    POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Networ ...

  4. UVALive 7712 Confusing Manuscript 字典树 查询与s的编辑距离为1的字符串数量

    /** 题目:UVALive 7712 Confusing Manuscript 链接:https://vjudge.net/problem/UVALive-7712 题意:给定n个不同的字符串,f( ...

  5. Linux基础介绍【第四篇】

    Linux文件和目录的属性及权限 命令: [root@oldboy ~]# ls -lhi total 40K 24973 -rw-------. 1 root root 1.1K Dec 10 16 ...

  6. ios项目里扒出来的json文件

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0. ...

  7. 云与备份之(1):VMware虚机备份和恢复

    本系列文章会介绍云与备份之间的关系,包括: (1)VMware 虚机备份和恢复 (2)KVM 虚机备份和恢复 (3)云与备份 (4)OpenStack 与备份 (5)公有云与备份 1. 与备份有关的V ...

  8. 卡通图像变形算法(Moving Least Squares)附源码

    本文介绍一种利用移动最小二乘法来实现图像变形的方法,该方法由用户指定图像中的控制点,并通过拖拽控制点来驱动图像变形.假设p为原图像中控制点的位置,q为拖拽后控制点的位置,我们利用移动最小二乘法来为原图 ...

  9. RHEL7 CentOS7 检查查看精简指令

    RHEL7 CentOS7 检查查看精简指令: //////////////////////////检查查看精简指令://///////////////////////////// ///////// ...

随机推荐

  1. 串的模式匹配,KMP算法

    串的模式匹配 现考虑一个常用操作,在字符串s(我们称为主串)中的第pos开始处往后查找,看在主串s中有没有和子串p相匹配的的,如果有,则返回字串p第一次出现的位置. 暴力求解 int Index(ch ...

  2. vue之多页面的开发

    我们平常用vue开发的时候总觉得vue好像就是专门为了单页面应用而诞生的,其实不是.因为vue在工程化开发的时候很依赖webpack,而webpack是将所有的资源整合到一块,弄成一个单页面.但是vu ...

  3. 通过python的selenium实现网站自动登陆留言

    from selenium import webdriver import time driver = webdriver.Chrome() driver.get('https://wordpress ...

  4. java读取串口-mfz-rxtx-2.2-20081207-win-x86

    1.下载jar包 RXTXcomm.jar 2.实现代码 package main; import java.awt.*; import java.awt.event.*; import java.i ...

  5. SQL学习——BETWEEN运算符

    原文链接 BETWEEN的作用 BETWEEN 操作符用于选取介于两个值之间的数据范围内的值. BETWEEN的边界 BETWEEN运算符选择给定范围内的值.值可以是数字,文本或日期. BETWEEN ...

  6. Rabbitmq各方法的作用详解

    exchange_declare('direct_logs', 'direct', false, false, false);// 这个是申明交换器,如果没有申明就给默认队列的这个交换器,而且发送的类 ...

  7. (备忘)Python字符串、元组、列表、字典互相转换的方法

    #1.字典 dict = {'name': 'Zara', 'age': 7, 'class': 'First'} #字典转为字符串,返回:<type 'str'> {'age': 7, ...

  8. Xadmin相关实现

    一,保存搜索条件(Save search conditions) kept conditions(保存条件)的应用场景比较常见,在我们查询时,可以实现多条件的筛选查询,比如:在淘宝上,选择了其中的一个 ...

  9. MyBatis-10-多对一处理

    10.多对一处理 多对一: 多个学生,对应一个老师 对于学生这边而言,关联...多个学生,关联一个老师[多对一] 对于老师而言,集合,一个老师又很多学生[一对多] SQL: CREATE TABLE ...

  10. redis主从+哨兵 安装配置一

    一.目的 实现redis的高可用. 二.同步过程 注意:当Master在后台把数据保存到快照文件完成之后,Master会把这个快照文件传送给Slave,而Slave则把内存清空后,加载该文件到内存中: ...