Hdu2437-Jerboas(取余数判重搜索)
2016-07-11 15:13
309 查看
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.
View Code
#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+7; const double eps=1e-7; const int maxn=1005; int N,M,start,mod; bool vis[maxn][maxn]; char road[maxn]; struct node { int val,rest,pos; node(int val=0,int rest=0,int pos=0):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=0,int v=0,int c=0):u(u),v(v),c(c){} }; vector<edge> G[maxn]; void init() { while(!que.empty()) que.pop(); for(int i=0;i<maxn;i++) G[i].clear(); memset(vis,false,sizeof(vis)); } void solve() { int ansl=INF,ansp=-1; que.push(node(0,0,start)); vis[0][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==0&&road[pos]=='P') //是P点更新答案 { if(val<ansl||(val==ansl&&pos<ansp)) { ansl=val; ansp=pos; } } int Size=G[pos].size(); for(int i=0;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==-1) printf("-1 -1\n"); else printf("%d %d\n",ansl,ansp); } int main() { int T,Case=0; scanf("%d",&T); while(T--) { scanf("%d%d%d%d",&N,&M,&start,&mod); scanf("%s",road+1); //输入 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 0; }
View Code
相关文章推荐
- css中的选择器
- 汉字转拼音(完全模式)
- 关于Other Link Flag的有关说明
- 更新统计信息三种方式
- __block 与 __weak的区别与使用
- Html5在ios上input标签自动填充内容移动光标至尾的方法
- php的debug相关函数用法示例
- linux 下ngnix启动,停止命令总结
- Centos之Lnmp的搭建
- 对MapReduce初步认识
- io流学习之字节流与字符流之间的转换
- io流学习之字节流与字符流之间的转换
- io流学习之字节流与字符流之间的转换
- io流学习之字节流与字符流之间的转换
- 简单而又少的数据缓存策略
- Swift: Alamofire -> http请求 & ObjectMapper -> 解析JSON
- android 开发赚钱
- GitBook 制作电子书详细教程
- 左右法则is interesting
- Linux C第三课