const int N=1e3+100;//顶点数
const int M=4e3+100;//边数
struct Edge
{
int to,w,next;
}edge[M];
int head[N],d[N],cnt;//链式前向星
bool vis[N];
void addedge(int u,int v,int w)
{
edge[cnt].to=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt++;
}
struct Node
{
int to,w;
Node(int TO,int W)
{
to=TO;
w=W;
}
bool operator<(const Node& a)const
{
return w>a.w;
}
};
void Dijkstra(int st)
{
priority_queueq;
memset(vis,false,sizeof(vis));
memset(d,inf,sizeof(d));
d[st]=0;
q.push(Node(st,0));
while(q.size())
{
Node cur=q.top();
int u=cur.to;
q.pop();
if(vis[u])
continue;
vis[u]=true;
for(int i=head[u];i!=-1;i=edge[i].next)//扫描出所有边
{
int v=edge[i].to;
int w=edge[i].w;
if(d[v]>d[u]+w)//更新
{
d[v]=d[u]+w;
q.push(Node(v,d[v]));
}
}
}
}
void init()
{
memset(head,-1,sizeof(head));
cnt=0;
}
template
struct Dij
{
const static int N=1e3+100;
const static int M=4e3+100;
struct Edge
{
int to,next;
T w;
}edge[M];
int head[N],cnt;//链式前向星
T d[N];
bool vis[N];
void addedge(int u,int v,T w)
{
edge[cnt].to=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt++;
}
struct Node
{
int to;
T w;
Node(int TO,T W)
{
to=TO;
w=W;
}
bool operator<(const Node& a)const
{
return w>a.w;
}
};
void Dijkstra(int st)
{
priority_queueq;
memset(vis,false,sizeof(vis));
memset(d,0x3f,sizeof(d));
d[st]=0;
q.push(Node(st,0));
while(q.size())
{
Node cur=q.top();
int u=cur.to;
q.pop();
if(vis[u])
continue;
vis[u]=true;
for(int i=head[u];i!=-1;i=edge[i].next)//扫描出所有边
{
int v=edge[i].to;
T w=edge[i].w;
if(d[v]>d[u]+w)//更新
{
d[v]=d[u]+w;
q.push(Node(v,d[v]));
}
}
}
}
void init()
{
memset(head,-1,sizeof(head));
cnt=0;
}
};
const int N=1e3+100;//顶点数
const int M=4e3+100;//边数
struct Edge
{
int to,w,next;
}edge[M];
int head[N],d[N],cnt;//链式前向星
bool vis[N];
void addedge(int u,int v,int w)
{
edge[cnt].to=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt++;
}
struct Node
{
int to,w;
Node(int TO,int W)
{
to=TO;
w=W;
}
bool operator<(const Node& a)const
{
return w>a.w;
}
};
void spfa(int st)
{
memset(vis,false,sizeof(vis));
memset(d,inf,sizeof(d));
d[st]=0;
vis[st]=true;
queueq;
q.push(st);
while(q.size())
{
int u=q.front();
q.pop();
vis[u]=false;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
int w=edge[i].w;
if(d[v]>d[u]+w)
{
d[v]=d[u]+w;
if(!vis[v])
{
q.push(v);
vis[v]=true;
}
}
}
}
}
void init()
{
memset(head,-1,sizeof(head));
cnt=0;
}
template
struct Spfa
{
const static int N=1e3+100;//顶点数
const static int M=4e3+100;//边数
struct Edge
{
int to,next;
T w;
}edge[M];
int head[N],cnt;//链式前向星
T d[N];
bool vis[N];
void addedge(int u,int v,T w)
{
edge[cnt].to=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt++;
}
struct Node
{
int to;
T w;
Node(int TO,T W)
{
to=TO;
w=W;
}
bool operator<(const Node& a)const
{
return w>a.w;
}
};
void spfa(int st)
{
memset(vis,false,sizeof(vis));
memset(d,0x3f,sizeof(d));
d[st]=0;
vis[st]=true;
queueq;
q.push(st);
while(q.size())
{
int u=q.front();
q.pop();
vis[u]=false;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
T w=edge[i].w;
if(d[v]>d[u]+w)
{
d[v]=d[u]+w;
if(!vis[v])
{
q.push(v);
vis[v]=true;
}
}
}
}
}
void init()
{
memset(head,-1,sizeof(head));
cnt=0;
}
};