把查分约束看出来,这道题其实就是最短路的一个模板题,下面给出两种实现方式:
第一种:Dijkstra+priority_queue+链式前向星(600ms)
第二种:基于stack优化的spfa+链式前向星(纯spfa会TLE,优化后500ms)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxe=150010;
const int maxv=30010;
const int INF=0x3f3f3f3f;
int head[maxv],cnt,dis[maxv];
bool visit[maxv];
struct Edge {
int to,val,next;
} edge[maxe];
struct Node{
int u,dis;
bool operator< (const Node rhs) const{
return this->dis>rhs.dis;
}
}now,temp;
void init() {
memset(head,-1,sizeof head);
cnt=0;
}
void addedge(int from,int to,int val) {
edge[cnt].to=to;
edge[cnt].val=val;
edge[cnt].next=head[from];
head[from]=cnt++;
}
void Dijkstra(int s,int n) {
for(int i=1;i<=n;i++)
dis[i]=I