传送门:计蒜客——百度地图导航
#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;
}