题目链接:https://cn.vjudge.net/contest/276233#problem/I

AC代码:

 #include<iostream>
#include<cstring>
#include<stack>
#include<iomanip>
#include<cmath>
#include<queue>
#include<algorithm>
#include<stdio.h>
using namespace std;
# define ll long long
# define inf 0x3f3f3f3f
const int maxn = +;
const int maxedge = 5e6+;
struct node
{
int to;
int nex;
int cost;
} edge[maxedge];
vector<int>w;
int head[maxn],pre[maxn],dis[maxn],vis[maxn];
int num;
void init()
{
num=;
for(int i=; i<=maxn; i++)
{
pre[i]=-inf;
dis[i]=inf;
head[i]=-;
}
}
void addedge(int fr,int to,int cost)
{
edge[num].to=to;
edge[num].cost=cost;
edge[num].nex=head[fr];
head[fr]=num++;
}
int spfa(int st,int ed)
{
queue<int>q;
vis[st]=;
dis[st]=;
q.push(st);
while(!q.empty())
{
int tmp=q.front();
q.pop();
vis[tmp]=;
for(int i=head[tmp]; i!=-; i=edge[i].nex)
{
int u=edge[i].to;
if(dis[u]>dis[tmp]+edge[i].cost)
{
dis[u]=dis[tmp]+edge[i].cost;
pre[u]=tmp;
if(vis[u])
continue;
vis[u]=;
q.push(u);
}
}
}
return dis[ed];
}
int main()
{
int k,n;
scanf("%d %d",&k,&n);
int u,v;
init();
int minx=inf,maxy=;
for(int i=; i<=n; i++)
{
scanf("%d %d",&u,&v);
if(u>v)
swap(u,v);
u+=;
v+=;
int len=v-u+;
addedge(v,u-,len);
addedge(u-,v,-min(len,k));
minx=min(minx,u-);
maxy=max(maxy,v);
}
for(int i=minx; i<maxy; i++)
{
addedge(minx-,i,);
addedge(i,i+,);
addedge(i+,i,);
}
addedge(minx,maxy,);
int ans=spfa(minx,maxy);
for(int i=minx+; i<=maxy; i++)
{
if(dis[i]!=dis[i-])
{
w.push_back(i);
}
}
int len=w.size();
printf("%d\n",len);
sort(w.begin(),w.end());
for(int i=; i<len; i++)
{
cout<<w[i]-<<endl;
} return ;
}
05-11 22:22
查看更多