最短路 vector不T

Posted daybreaking

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了最短路 vector不T相关的知识,希望对你有一定的参考价值。

  1 //A vector不T
  2 // 最短路    SPFA 
  3 //  in数组
  4 
  5 #include<cstdio>
  6 #include<cstdlib>
  7 #include<cstring>
  8 #include<cmath>
  9 #include<algorithm>
 10 #include<queue>
 11 #include<stack>
 12 #include<vector>
 13 #include<deque>
 14 #include<map>
 15 #include<iostream>
 16 using namespace std;
 17 typedef long long  LL;
 18 const double pi=acos(-1.0);
 19 const double e=exp(1);
 20 //const int MAXN =2e5+10;
 21 const int N =332748118;
 22 
 23 
 24 struct edge
 25 {
 26     LL to,w;
 27 }two[100009];
 28 
 29 LL head[700009];
 30 LL dis[200009];
 31 LL in[200009];
 32 LL check[200009];
 33 
 34 vector<struct con > con[700009];
 35 queue<LL > qq;
 36 LL n;
 37 
 38 void init()
 39 {
 40     LL i,p,j;
 41 
 42     memset(head,-1,sizeof(head));
 43     memset(check,0,sizeof(check));
 44     for(i = 2; i <= n; i++)
 45     {
 46         dis[i] = 9999999999;
 47     }
 48 
 49 }
 50 
 51 void BFS()
 52 {
 53     LL i,p,j;
 54     LL x;
 55 
 56 
 57     qq.push(1);
 58     check[1] = 1;
 59     dis[1] = 0;
 60 
 61     while(!qq.empty())
 62     {
 63         x = qq.front();
 64         check[x] = 0;
 65         qq.pop();
 66 
 67         for(i = 0; i < con[x].size(); i++)
 68         {
 69             to = con[x][i].to;
 70             w = con[x][i].w;
 71             if(dis[to] > dis[x] + w)
 72             {
 73                 dis[to] = dis[x] + w;
 74                 in[to] = 1;
 75 
 76                 if(check[to] == 0)
 77                 {
 78                     check[to] = 1;
 79                     qq.push(to);
 80                 }
 81             }
 82             else if(dis[to] == dis[x] + w)
 83             {
 84                 in[to] ++;
 85             }
 86         }
 87 /*
 88         for(i = head[x]; i != -1; i = edge[i].next)
 89         {
 90 
 91           //  cout << "x: " << x << "to: " << edge[i].to << endl;
 92             if(dis[edge[i].to] > dis[x] + edge[i].w)
 93             {
 94                 dis[edge[i].to] = dis[x] + edge[i].w;
 95                 in[edge[i].to] = 1;
 96                 if(check[edge[i].to] == 0)  //没入队
 97                 {
 98                     check[ edge[i].to ] = 1;
 99                     qq.push( edge[i].to );
100                 }
101             }
102             else if(dis[edge[i].to] == dis[x] + edge[i].w)
103             {
104                 in[edge[i].to]++;
105             }
106 
107         }
108 */
109     }
110 //
111 //    for(i = 1; i <= n; i++)
112 //    {
113 //        cout << i << ": " << dis[i] << endl;
114 //    }
115 }
116 
117 int main()
118 {
119     LL i,p,j,t;
120     LL m,k;
121     LL a,b,c,cnt = 0;
122     struct edge mid;
123     scanf("%lld%lld%lld",&n,&m,&k);
124 
125     init();
126 
127     while(m--)
128     {
129         scanf("%lld%lld%lld",&a,&b,&c);
130 
131         mid.to = b;
132         mid.w = c;
133         con[a].push_up(mid);
134 
135         mid.to = a;
136         con[b].push_up(mid);
137 /*
138         edge[cnt].w = c;
139         edge[cnt].to = b;
140         edge[cnt].next = head[a];
141         head[a] = cnt++;
142 
143         edge[cnt].w = c;
144         edge[cnt].to = a;
145         edge[cnt].next = head[b];
146         head[b] = cnt++;
147 */
148     }
149 
150     for(i = 0; i < k; i++)
151     {
152         scanf("%lld%lld",&b,&c);
153 
154         two[i].to = b;
155         two[i].w = c;
156 
157         mid.to =  b;
158         mid.w = c;
159         con[1].push_up(mid);
160 /*
161         edge[cnt].w =  c;
162         edge[cnt].to = b;
163         edge[cnt].next = head[1];
164         head[1] = cnt++;
165 */
166     }
167 
168     BFS();
169     LL ans = 0;
170     for(i = 0; i < k; i++)
171     {
172         if(dis[ two[i].to ] < two[i].w)
173         {
174             ans ++;
175         }
176         else if(dis[ two[i].to ] == two[i].w && in[ two[i].to ] > 1)
177         {
178             ans++;
179             in[ two[i].to ]--;
180         }
181        // cout << "i: " << con[i].to << " dis: " << dis[ con[i].to ] << " in: " << in[ con[i].to ] << endl;
182     }
183     printf("%lld
",ans);
184     return 0;
185 
186 }

 

以上是关于最短路 vector不T的主要内容,如果未能解决你的问题,请参考以下文章

最短路专题(不定期更新)

Luogu 1144 最短路计数

最短路的三种写法

最短路径

次短路

4456: [Zjoi2016]旅行者|分治+最短路