一道分层图的水题,觉得分层图这个玩意非常妙,于是就水了一波。。。
记记好是无向图。。。向图。。。图。。。
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=1000005;
const int maxm=5000005;
typedef pair<int,int>node;
struct edge
{
int to,next,val;
}e[maxm];
struct init
{
int u,v,val;
}q[maxm];
int n,m,k,Begin,End,cnt;
int head[maxn],dist[maxn];
bool vst[maxn];
int read()
{
int x=0;char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return x;
}
void insert(int a,int b,int c)
{
e[++cnt].val=c;e[cnt].to=b;e[cnt].next=head[a];head[a]=cnt;
}
void Dijkstra()
{
priority_queue<node,vector<node>,greater<node> >q;
q.push((node){0,Begin});
memset(dist,127/3,sizeof dist);
dist[Begin]=0;
while(!q.empty())
{
int u=q.top().second;
q.pop();
if(vst[u])continue;
vst[u]=true;
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].to;
if(dist[u]+e[i].val<dist[v])
{
dist[v]=dist[u]+e[i].val;
q.push((node){dist[v],v});
}
}
}
}
int main()
{
n=read(),m=read(),k=read();
Begin=read()+1,End=read()+1;
for(int i=1;i<=m;i++)
q[i].u=read()+1,q[i].v=read()+1,q[i].val=read();
for(int i=0;i<=k;i++)
for(int j=1;j<=m;j++)
insert(i*n+q[j].u,i*n+q[j].v,q[j].val),insert(i*n+q[j].v,i*n+q[j].u,q[j].val);
for(int i=0;i<k;i++)
for(int j=1;j<=m;j++)
insert(i*n+q[j].u,(i+1)*n+q[j].v,0),insert(i*n+q[j].v,(i+1)*n+q[j].u,0);
Dijkstra();
printf("%d",dist[End+k*n]);
return 0;
}
还可以拿类似于dp的思想水上一波
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=100005;
const int maxm=500005;
struct node
{
int dist,to,k;
bool operator > (const node&tmp)const
{
if(dist==tmp.dist)
{
if(to==tmp.to)return k>tmp.k;
return to>tmp.to;
}
return dist>tmp.dist;
}
};
struct edge
{
int to,next,val;
}e[maxm];
int n,m,k,Begin,End,cnt;
int head[maxn],dist[12][maxn];
bool vst[12][maxn];
int read()
{
int x=0;char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return x;
}
void insert(int a,int b,int c)
{
e[++cnt].val=c;e[cnt].to=b;e[cnt].next=head[a];head[a]=cnt;
}
void Dijkstra()
{
priority_queue<node,vector<node>,greater<node> >q;
q.push((node){0,Begin,0});
memset(dist,127/3,sizeof dist);
dist[0][Begin]=0;
while(!q.empty())
{
int u=q.top().to;
int now=q.top().k;
q.pop();
if(vst[now][u])continue;
vst[now][u]=true;
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].to;
if(dist[now][u]+e[i].val<dist[now][v])
{
dist[now][v]=dist[now][u]+e[i].val;
q.push((node){dist[now][v],v,now});
}
if(dist[now+1][v]>dist[now][u]&&now<k)
{
dist[now+1][v]=dist[now][u];
q.push((node){dist[now+1][v],v,now+1});
}
}
}
}
int main()
{
n=read(),m=read(),k=read();
Begin=read()+1,End=read()+1;
for(int i=1,u,v,val;i<=m;i++)
{
u=read()+1,v=read()+1,val=read();
insert(u,v,val),
insert(v,u,val);
}
Dijkstra();
printf("%d",dist[k][End]);
return 0;
}
跟OTZ@27rabbit学会了写对拍器
:loop
rand.exe
1.exe
2.exe
fc 1.txt 2.txt
if not errorlevel 1 goto loop
pause