HDU-2437-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. 


题意:

给你n个洞穴,有的是临时的有的是永久的,给定一个起点,各处m个洞穴之间的连接关系(单向)及其路径,给出一个k

求从起点(临时洞穴)到任意一个永久洞穴的最短路径,并且此路径长度得是k的倍数,如果有多解,求序号最小的目的地,输出路径长度和到达洞穴的序号。


解题思路:

构建一个邻接表,从起点开始BFS搜索,每个节点的状态用vis[x][mod]表示,如果当前节点曾经存在过这个mod(路径%k),那么不录入该节点(取余判重),BFS退出条件是当前节点路径大于最优解的路径,再次最优条件下如果有多解,按字典序更新目的地的序号,详见代码:

#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=0x3f3f3f3f;
const int maxn=1005;
const int maxm=20005;
char g[maxn];
int fir[maxn];
int u[maxm], v[maxm], w[maxm], nex[maxm];
int e_max;
int m,n,s,x;
int ans1,ans2;
inline void init()
{
        ans1=-1;
        ans2=-1;
        memset(fir, -1, sizeof fir);
        e_max = 0;
}
inline void add_edge(int s, int t, int c)
 {
        int e = e_max++;
        u[e] = s;
        v[e] = t;
        w[e] = c;
        nex[e] = fir[u[e]];
        fir[u[e]] = e;
}
int vis[maxn][1000];
struct Node
{
        int cur,dist;
        Node(int cur1,int dist1):cur(cur1),dist(dist1){}
        bool operator<(const Node&a)const
        {
                return a.dist<dist;
        }
};

priority_queue<Node>q;
void bfs()
{
        memset(vis,0,sizeof vis);
        while(!q.empty())
        {
                q.pop();
        }
        vis[s][0]=1;
        q.push(Node(s,0));
        int step=INF;
        while(!q.empty())
        {
                Node now=q.top();
                q.pop();
                if(now.dist>step)
                        return;
                if(g[now.cur]=='P'&&now.dist%x==0)
                {
                        if(now.dist<step)
                        {
                                step=now.dist;
                                ans1=now.dist;
                                ans2=now.cur;
                        }
                        if(now.dist==step&&now.cur<ans2)
                        {
                                ans2=now.cur;
                        }
                }
                for (int e = fir[now.cur]; ~e; e = nex[e])
                {
                        if( !vis[v[e]][(now.dist+w[e])%x] )
                        {
                                q.push(Node(v[e],now.dist+w[e]));
                                vis[v[e]][(now.dist+w[e])%x]=1;
                        }
                }
        }
}
int main()
{
        int T,a,b,c;
        cin>>T;
        int w=0;
        while(T--)
        {
                init();
                cin>>n>>m>>s>>x;
                for(int i=1;i<=n;i++)
                        cin>>g[i];
                for(int i=1;i<=m;i++)
                {
                       cin>>a>>b>>c;
                       add_edge(a,b,c);
                }
                cout<<"Case "<<++w<<": ";
                bfs();
                cout<<ans1<<' ';
                cout<<ans2<<endl;
        }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值