dijkstra TLE

P1346 电车

```cpp #include<stdio.h> #include<queue> #include<cstring> using namespace std; const int N=9999; int n,a,b,vis[N],m,x; struct Edge { int next; int v; int to; }edge[N<<1]; int head[N],tot; inline void add_edge(int u,int to,int v) { edge[++tot].next=head[u]; head[u]=tot; edge[tot].to=to; edge[tot].v=v; } struct node { int u; int dis; bool operator <(const node &a)const{ return dis>a.dis; } }; int dijkstra() { priority_queue<node> q; node now={a,0}; q.push(now); while(!q.empty()) { now=q.top(); q.pop(); if(vis[now.u]) continue; if(now.u==b)return now.dis; vis[now.u]=1; for(int i=head[now.u];i;i=edge[i].next) if(!vis[edge[i].to]) { q.push((node){edge[i].to,edge[i].v+now.dis}); } } return -1; } int main() { scanf("%d%d%d",&n,&a,&b); for(int i=1;i<=n;i++) { scanf("%d",&m); for(int j=1;j<=m;j++) { scanf("%d",&x); add_edge(i,x,j==1?0:1); } } printf("%d\n",dijkstra()); return 0; } ``` 吸口氧就跑得飞快
by cmll02 @ 2020-02-12 08:10:03


边太少
by cmll02 @ 2020-02-12 08:10:15


@[鸿海1001](/user/218037)
by cmll02 @ 2020-02-12 08:19:25


emmmmm 搞了一晚上
by 鸿海1001 @ 2020-02-12 10:10:12


@[shygo_cmll02](/user/171487)
by 鸿海1001 @ 2020-02-12 10:10:20


@[鸿海1001](/user/218037) 您太强了stOrz
by cmll02 @ 2020-02-12 11:16:46


|