适用范围:计算正权图上的单源最短路(Single-Source Shortest Paths,SSSP)。同时适用于有向图和无向图。
模板题:P4779 【模板】单源最短路径(标准版)
#include
using namespace std;
const int maxn=1e6+100;
const int INF=0x7fffffff;
struct Dijkstra{
struct Edge{
int from, to, dist;
Edge(int u,int v,int d):from(u),to(v),dist(d) {}
};
struct HeapNode{
int d,u;
bool operator < (const HeapNode& rhs) const {
return d > rhs.d;
}
};
int n,m;
vector edges;
vector G[maxn];
bool done[maxn];//是否已永久标记
int d[maxn];//s到各个点的距离
int p[maxn];//最短路中的上一条弧
void init(int n){
this->n=n;
this->m=m;
for(int i=0;i Q;
for(int i=1;i<=n;i++) d[i]=INF;
d[s]=0;
memset(done,0,sizeof(done));
Q.push((HeapNode){0,s});
while(!Q.empty()){
HeapNode x=Q.top(); Q.pop();
int u=x.u;
if(done[u]) continue;
done[u]=true;
for(int i=0;id[u]+e.dist){
d[e.to]=d[u]+e.dist;
p[e.to]=G[u][i];
Q.push((HeapNode){d[e.to],e.to});
}
}
}
}
};
int n,m,s;
Dijkstra D;
void read_input(){
int u,v,d;
cin>>n>>m>>s;
D.init(n);
for(int i=0;i>u>>v>>d;
D.AddEdge(u,v,d);
}
}
void solve(){
D.dijkstra(s);
for(int i=1;i<=n;i++) cout<
如果想要保证走的时间最短,就应该使用最短路,我们从一个点出发到一个点时,以天数为第一关键字,当前天数所剩时间为第二关键字进行优先队列的标准进行 Dij,如果当前天的时间加上走这条的时间小于等于 12 则可以转移,如果大于则无法转移。
但是这并不能覆盖样例的等候情况,因此我们需要判断是否能够在一个地点等一天,所以我们直接在判断之后进行一次下一天的过渡,在转移过程中记录相关信息,最后到终点时直接输出即可。
#include
#include
#include
#include
using namespace std;
int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-') f=-f;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
struct node{
int da,ti,np;
friend bool operator < (node x,node y)
{
if(x.da==y.da) return x.ti>y.ti;
return x.da>y.da;
}
};//天数,天数相同比较时间
priority_queue q;
int hpla[110],st,to[2010],nxt[2010],head[2010],val[2010],fl[2010],cnt,vis[110][2][20],pre[110],hpt[110],hpti[110];
int ansp[110],ansx[110],anst[110],ansti[110];
void addedge(int x,int y,int z,int ff)
{
to[++cnt]=y;
nxt[cnt]=head[x];
head[x]=cnt;
val[cnt]=z;
fl[cnt]=ff;//正图还是反图
}
void out(int x,int &nc)
{
// cout<=0;i--)
{
cout<