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 ...
随机推荐
- jQuery----blur()方法
当元素失去焦点时发生 blur 事件. blur() 函数触发 blur 事件,或者如果设置了 function 参数,该函数也可规定当发生 blur 事件时执行的代码. 提示:早前,blur 事件仅 ...
- JS~对img标签进行优化,使用onerror进行默认图像显示
对于网站图像的不显示有很多原因,网络问题,文件本身问题,文件URL问题等,而当图像加载失败时会触发onerror这个事件,我们利用这点,可以有效的避免图像加载失败的尴尬! JS代码 //图像加载出错时 ...
- Spark Accumulators
概述 Accumulator即累加器,与Mapreduce counter的应用场景差不多,都能很好地观察task在运行期间的数据变化,Spark中的Accumulator各task可以对Accumu ...
- Android BLE开发之Android手机与BLE终端通信
本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处! 近期穿戴设备发展得非常火.把相关技术也带旺了,当中一项是BLE(Bluetooth Low Energy).B ...
- HDU 4760 Good FireWall 完好Trie题解
本题乍看像是线段树之类的区间操作,只是由于仅仅是须要查找ip的前缀,故此事实上是使用Trie来做. 挺高难度的Trie应用,做完这道题之后说明Trie功力有一定火候了. 这里的Trie使用到了Dele ...
- Android动态加载jar/dex
前言 在目前的软硬件环境下,Native App与Web App在用户体验上有着明显的优势,但在实际项目中有些会因为业务的频繁变更而频繁的升级客户端,造成较差的用户体验,而这也恰恰是Web App的优 ...
- 一致性哈希与java实现
一致性哈希算法是分布式系统中常用的算法.比如,一个分布式的存储系统,要将数据存储到具体的节点上,如果采用普通的hash方法,将数据映射到具体的节点上,如key%N,key是数据的key,N是机器节点数 ...
- uva11292贪心基础题目
C - 贪心 基础 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bi ...
- 关于(x&y)+((x^y)>>1)的探究
今天在程序员面试宝典上看到 int f(int x int y ) { return (x&y)+((x^y)>>1) } f(729,271) 结果为500 从式子中可以看出分为 ...
- c# 中的线程和同步
一.新建线程的3种方法 a)异步委托:b)Thread类:c)线程池: 二.异步委托 1.简单使用,检查委托是否完成其任务 a) 通过 BeginInvoke() 的返回值IAsyncResult ...