Hdu2437-Jerboas(取余数判重搜索)
Jerboas are small desert-living animals, which resemble mice with a long tufted tail and very long hind legs. Jerboas shelter in well-hidden burrows. They create two types of burrow: temporary and permanent. The temporary burrows are plain tubes while the permanent burrows are sealed with a plug of sand to keep heat out and moisture in.
As far as we know, jerboa burrows in the desert are connected with one-way tunnels. What's more, for some unknown reasons, it's true that start from any burrow, follows the tunnels you can not go back to the starting burrow. Summer means last-minute of offers on good times, so of course jerboas could not stay behind. One day, a little jerboa Alice who lived in a temporary burrow S wants to migrate to a permanent one. There are different routes she can take, but Alice is so odd that she only selects those whose total travel distances is a multiple of K. Among all routes that Alice may select, we are interested in the shortest one. Can you help to find it out? Of course different routes may lead to different destinations.
Each test case starts with four integers in the first line: N, M, S, K. N is the number of burrows in the desert (burrows are numbered with 1, 2, …, N); M is the number of tunnels connecting the burrows;
S is where Alice lived and K is as described above. (0 < N <= 1000, 0 <= M <= 20000, 0 < S <= N, 0 < K <= 1000) The second line contains N characters each could be ‘T’ or ‘P’. The i-th character specifying the type of the burrow i. ‘T’ means temporary burrow, ‘P’ means permanent burrow. It’s guaranteed that the S-th character is ‘T’. Next follow M lines, each line with 3 integers A, B, C. Specifying that there is a tunnel from burrow A to burrow B, and its length is C. (0 < A, B <= N, A != B, 0 < C < 40000)
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<utility>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<iterator>
#include<stack>
using namespace std;
const int INF=1e9+;
const double eps=1e-;
const int maxn=;
int N,M,start,mod;
bool vis[maxn][maxn];
char road[maxn];
struct node
{
int val,rest,pos;
node(int val=,int rest=,int pos=):val(val),rest(rest),pos(pos){}
bool operator < (const node& t) const{ return val>t.val; }
};
priority_queue<node> que;
struct edge
{
int u,v,c;
edge(int u=,int v=,int c=):u(u),v(v),c(c){}
};
vector<edge> G[maxn];
void init()
{
while(!que.empty()) que.pop();
for(int i=;i<maxn;i++) G[i].clear();
memset(vis,false,sizeof(vis));
}
void solve()
{
int ansl=INF,ansp=-;
que.push(node(,,start));
vis[][start]=true;
while(!que.empty())
{
node t=que.top(); que.pop();
int val=t.val,rest=t.rest,pos=t.pos;
if(val>ansl) break; //路径长度大了
if(rest==&&road[pos]=='P') //是P点更新答案
{
if(val<ansl||(val==ansl&&pos<ansp))
{
ansl=val;
ansp=pos;
}
}
int Size=G[pos].size();
for(int i=;i<Size;i++)
{
edge& e=G[pos][i];
int v=e.v,c=e.c+val;
if(vis[c%mod][v]) continue;
vis[c%mod][v]=true;
que.push(node(c,c%mod,v));
}
}
if(ansp==-) printf("-1 -1\n");
else printf("%d %d\n",ansl,ansp);
}
int main()
{
int T,Case=;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d",&N,&M,&start,&mod);
scanf("%s",road+); //输入
init();
int u,v,c;
while(M--)
{
scanf("%d%d%d",&u,&v,&c);
G[u].push_back(edge(u,v,c));//边
}
printf("Case %d: ",++Case);
solve();
}
return ;
}
Hdu2437-Jerboas(取余数判重搜索)的更多相关文章
- hdu 1226 bfs+余数判重+大数取余
题目: 超级密码 Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- poj 1465 Multiple(bfs+余数判重)
题意:给出m个数字,要求组合成能够被n整除的最小十进制数. 分析:用到了余数判重,在这里我详细的解释了.其它就没有什么了. #include<cstdio> #include<cma ...
- hdu 1226 超级密码(bfs+余数判重)
题意:略过 分析:用m个数字组成一个能够被n整除的c进制数的最小值,实际上本题的关键就在于这个最小值上. 首先确定我们的思路是从小到大寻找.先查看一位数,即查看着m个数字是否能被n整除:若不能,就查 ...
- hdu1664 bfs+余数判重
input n 不超过50个例子,n==0结束输入 Sample Input 7 15 16 101 0 output 最少个不同数字的n的倍数的x,若不同数字个数一样,输出最小的x Sample O ...
- Problem H. The Fence 通过取余判重,求得某个区间的某些个数为某个数的倍数。
/** 题目:Problem H. The Fence 链接:https://vjudge.net/problem/Gym-101090H 题意:给定一个字符串,只有0或者1: 问:假如两个不同的1之 ...
- BFS(四):搜索状态判重
在采用广度优先算法进行搜索时,一个需要重点注意的是在搜索过程中判重和去重.前面介绍的几个例子中,判重都较简单,如采用vis[]数组,若vis[i]==0,则i未访问过,i入队列:若vis[i]!=0, ...
- 逆向bfs搜索打表+康拓判重
HDU 1043八数码问题 八数码,就是1~8加上一个空格的九宫格,这道题以及这个游戏的目标就是把九宫格还原到从左到右从上到下是1~8然后最后是空格. 没了解康托展开之前,这道题怎么想都觉得很棘手,直 ...
- UVA 10651 Pebble Solitaire(bfs + 哈希判重(记忆化搜索?))
Problem A Pebble Solitaire Input: standard input Output: standard output Time Limit: 1 second Pebble ...
- 【DFS+小操作判重】【HDU2610+HDU2611】Sequence
题意 2610 按照长度优先 位置次之 输出所有不递减序列 2611 按照长度优先 大小次之 输出所有不递减序列 题解不写了 来源于http://www.cnblogs.com/wally/archi ...
随机推荐
- J2EE学习路线
第一部分: JAVA语言基础知识.包括异常.IO流.多线程.集合类.数据库.(切记基础知识一定要时时刻刻巩固,注意,如果你是想以最快速度学习J2EE,关于Java中的Swing知识点,就只做了解) ...
- JAVA并发实现五(生产者和消费者模式wait和notify方式实现)
package com.subject01; import java.util.PriorityQueue; /** * 通过wait和notify 实现 * 生产者-消费者模型:当队列满时,生产者需 ...
- hdu 5159 Card (期望)
Problem Description There are x cards on the desk, they are numbered from 1 to x. The score of the c ...
- eclipse java 配置
1.eclipse菜单 - Window - Preferences- Java - Installed JREs 2.eclipse菜单 - Window - Preferences- Java - ...
- 慕课linux学习笔记(一)centOS的安装
在VMware8上安装centos6.3 准备的文件 新建虚拟机 选择新建一个空的虚拟机 选择linux和centos 分配20G的硬盘空间 ' 修改配置 调整内存空间 桥接:虚拟机和真实机通讯使用的 ...
- 监控工具cacti
一. 安装 cacti服务端 1. 首先要安装epel扩展源yum install -y epel-release2. (lamp)然后分别安装httpd.php.mysqlyum install - ...
- html游戏引擎,createJs框架
声明:本文为原创文章,如需转载,请注明来源WAxes,谢谢! createJs网上的中文教程挺少的,以前UC有个Xcanvas的论坛有createJs的详细教程,但是随着XCanvas团队的解散,那个 ...
- Js弹性漂浮广告代码
<html><head><meta http-equiv="Content-Type" content="text/html; charse ...
- arc engine - IName
IName对象是一个代表性对象.通过使用IName对象,可以访问它所代表的对象的一些基本属性,而不用将整个对象调入内存.我们用IWorkspace 获得一个Workspace,那可是会调入内存的,而I ...
- silverlight+wcf 项目 silverlight获得web程序的参数
silverlight 可以通过属性InitParams 获得参数,如果参数是动态的需要web程序传递的,具体操作如下: web程序后台:AppID,USERID需要的参数 this.frmRepor ...