计蒜之道复赛D题—— 百度地图导航

传送门计蒜客——百度地图导航

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <queue>
#include <string>
#include <map>
using namespace std;
#define LL long long
#define N 30005
#define M 1000005
const LL INF = 0x6f6f6f6f6f6f6f6f;
/*
题意:
给n个城市,m个城市群,
两种通道:
1、城市间通道
2、城市群间通道,若城市群a有,ka个城市,成熟群b,kb个城市
   则相当于在两个群中的所有城市建 ka*kb条路
3、求给定的两点间最短距离
思路:
1、将城市群拆点,分为入点和出点,
 单向路:
   城市群的出点链接其包含的城市,入点链接其他城市群,
   城市链接所在城市群的入点
 双向路:
   城市间互联
2、使用 优先队列 优化的 dijkstra 算法。
*/
const double eps = 1e-10;
const int mod = 110119;
struct edge
{
    int v,next,dis;
}e[M];
 int n,mm,m;
LL dis[N<<1];
int head[N<<1];
bool has[N<<1];
struct node
{
    int index;
    LL cost;
    node(int Index,LL Cost):index(Index),cost(Cost){}
    bool operator <(const node &x)const{
       return  x.cost < cost;
    }
};
int tot;
inline void add(int u,int v,int c)
{
    e[tot].v = v;
    e[tot].dis = c;
    e[tot].next = head[u];
    head[u] = tot++;
}
inline void dijstra(int s,int t)   //优先队列优化的dijkstra废
{
    priority_queue<node>que;
    memset(dis,INF,sizeof(dis));
    dis[s] = 0;
    que.push(node(s,(LL)0));
    while(!que.empty()){
        node w = que.top(); que.pop();
        int u = w.index;
        if(has[u]) continue;
        has[u] = 1;
        for(int i=head[u];~i;i=e[i].next){
            int v = e[i].v;
            if(has[v]) continue;
            if(dis[v] > w.cost + e[i].dis){
                dis[v] = w.cost + e[i].dis;
                que.push(node(v,dis[v]));
            }
        }
    }
    if(dis[t] == INF) printf("-1\n");
    else printf("%lld\n",dis[t]);
}
int main()
{
    scanf("%d%d",&n,&mm);
    memset(head,-1,sizeof(head));
    int tot  = 0;
    for(int i=1;i<=mm;i++){
        int ki;
        scanf("%d",&ki);
        //cout<<"*"<<n+i<<endl;
        while(ki--){
            int a;
            scanf("%d",&a);
            add(n+2*i,a,0);    //城市群的出点链接其包含的城市
            add(a,n+2*i-1,0);  //城市链接所在城市群的入点
        }
    }
    scanf("%d",&m);
    while(m--){
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        add(a,b,c);            //城市间互联
        add(b,a,c);
    }
    scanf("%d",&m);
    while(m--){
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
           add(n+2*b-1,n+2*a,c);  //城市群的入点链接其他城市群
           add(n+2*a-1,n+2*b,c);  //城市群的入点链接其他城市群
    }
    int s,t;
    scanf("%d%d",&s,&t);
    dijstra(s,t);
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值