计蒜客 2017复赛 百度地图导航
dijstra + 堆优化 + 反向优先队列
这道题的建图很重要,感觉很厉害
1 #include<cstdio>
2 #include<cstring>
3 #include<queue>
4 #include<vector>
5 #include<stack>
6 #include<set>
7 #include<iostream>
8 #include<algorithm>
9 using namespace std;
10
11 struct node
12 {
13 int k;
14 long long w;
15 };
16 priority_queue<node>Q;
17 bool operator < ( const node & d1, const node & d2 ) { return d1.w > d2.w; }
18 vector<vector<node> >G;
19 bool vis[60001];
20
21
22 void dij(int s,int t)
23 {
24 memset(vis,false,sizeof(vis));
25 node tempnode;
26 tempnode.k = s;
27 tempnode.w = 0;
28 Q.push(tempnode);
29 while(!Q.empty())
30 {
31 tempnode = Q.top(); Q.pop();
32 vis[tempnode.k] = true;
33 if(tempnode.k == t)
34 {
35 printf("%lld",tempnode.w);
36 return;
37 }
38 for(int i=0,j=G[tempnode.k].size();i<j;i++)
39 {
40 node nexnode; nexnode.k = G[tempnode.k][i].k;
41 if(vis[nexnode.k]) continue;
42 nexnode.w = tempnode.w + G[tempnode.k][i].w;
43 Q.push(nexnode);
44 }
45 }
46 printf("-1");
47 }
48
49 int main()
50 {
51 int n,m;
52 node tempnode;
53 scanf("%d%d",&n,&m);
54 G.resize(n+m*2+1);
55 int cnt = n+1;
56 for(int i=1;i<=m;i++)
57 {
58 int k,ki;
59 scanf("%d",&k);
60 for(int j=0;j<k;j++)
61 {
62 scanf("%d",&ki);
63 tempnode.k = n+(i<<1)-1;
64 tempnode.w = 0;
65 G[ki].push_back(tempnode);
66 tempnode.k = ki;
67 G[n+(i<<1)].push_back(tempnode);
68 }
69 }
70 int m1; scanf("%d",&m1);
71 int a,b,c;
72 for(int i=0;i<m1;i++)
73 {
74 scanf("%d%d%d",&a,&b,&c);
75 tempnode.k = b;
76 tempnode.w = c;
77 G[a].push_back(tempnode);
78 tempnode.k = a;
79 G[b].push_back(tempnode);
80 }
81 int m2; cin>>m2;
82 for(int i=0;i<m2;i++)
83 {
84 scanf("%d%d%d",&a,&b,&c);
85 tempnode.k = (b<<1)+n;
86 tempnode.w = c;
87 G[(a<<1)+n-1].push_back(tempnode);
88 tempnode.k = (a<<1)+n;
89 G[(b<<1)+n-1].push_back(tempnode);
90 }
91 int s,t;
92 cin>>s>>t;
93 dij(s,t);
94 return 0;
95 }