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 ...
随机推荐
- Mac系统升级到10.9(mavericks)时安装php扩展问题解决(转)
问题一: 执行执行 phpize 报错: grep: /usr/include/php/main/php.h: No such file or directory grep: /usr/include ...
- python手记(39)
#!/usr/bin/env python #-*- coding: utf-8 -*- #code:myhaspl@qq.com import cv2 import numpy as np fn=& ...
- hdu 5410 CRB and His Birthday(混合背包)
Problem Description Today is CRB's birthday. His mom decided to buy many presents for her lovely son ...
- iOS 提示音播放
首先找到对应的素材 音频文件 写一个类继承 NSObject 命名为AudioUtil 导入支撑文件 #import <AVFoundation/AVFoundation.h> #impo ...
- 安装centos6.3
废话少说,今天安装镜像文件.版本为centos6.3 1.首先,我们已经创建了一个空的虚拟机,此时,打开虚拟机,选择的镜像文件,点击ok自己下载 2.点击绿色的三角箭头,你会看到下面页面.(如果报错T ...
- Sqlserver统计语句
--查看被缓存的查询计划 SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED st.text AS [SQL] , cp.cacheobjtype , c ...
- Code First 数据注释--DatabaseGenerated
EF中可以使用DatabaseGenerated设置数据库字段的属性,它有三个枚举值:Computed.None.Identity Computed 表示这一列是计算所得 None 不做处理 Iden ...
- Java ----------- SQL语句总结(更新中。。。。。。)
#对数据库的操作 *创建数据库 CREATE DATABASE database_name:database_name为创建的数据库的变量名称. #对表的操作
- "ORA-00942: 表或视图不存在 "的原因和解决方法[转]
采用Oracle数据库,使用Powerdesigner设计,生成Sql文件导入后查询出现“ORA-00942: 表或视图不存在 ”,很是郁闷,这个问题以前出现过,当初解决了,但因好久没有使用,这次竟然 ...
- Oracle异常的抛出处理
--一异常处理的代码 --sqlcode 异常编号 --sqlerrm 信号字符串 /* 在plsql 块中格式 Declare 变量 Begin 代码块 EXCEPTION when 异常的名称 t ...