https://nanti.jisuanke.com/t/31001

题意 可以把k条边的权值变为0,求s到t的最短路

解析  分层最短路  我们建立k+1层图 层与层之间边权为0,i 向 i+1层转移,代表用了一条免费边。

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define huan printf("\n");
#define debug(a,b) cout<<a<<" "<<b<<" ";
using namespace std;
const int maxn=3e6+,inf=0x3f3f3f3f;
typedef long long ll;
typedef pair<int,int> pii;
int n,m,k,st,ed,cnt,head[maxn],vis[maxn];
ll dis[maxn];
struct node
{
ll from,to,val,next;
} edge[maxn<<];
struct element
{
ll val,now;
};
bool operator < (element a,element b)
{
if(a.val==b.val)
return a.now<b.now;
return a.val>b.val;
}
void dijikstra(int s,int e)
{
priority_queue<element>q;
memset(dis,0x3f,sizeof(dis));
dis[s]=;
q.push(element{,s});
while(!q.empty())
{
element u=q.top();
q.pop();
if(vis[u.now])
continue;
vis[u.now]=;
for(int i=head[u.now]; i!=-; i=edge[i].next)
{
int to=edge[i].to;
if(dis[u.now]+edge[i].val<dis[to])
{
dis[to]=dis[u.now]+edge[i].val;
q.push(element{dis[to],to});
}
}
}
ll ans=1e18;
for(int i=; i<=k; i++)
{
if(ans>dis[e+i*n])
ans=dis[e+i*n];
}
printf("%lld\n",ans);
}
void init()
{
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis));
cnt=;
}
void edgeadd(ll from,ll to,ll val)
{
edge[cnt].from=from;
edge[cnt].to=to;
edge[cnt].val=val;
edge[cnt].next=head[from];
head[from]=cnt++;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
init();
scanf("%d%d%d",&n,&m,&k);
st=,ed=n;
for(int i=; i<=m; i++)
{
ll x,y,z;
scanf("%lld%lld%lld",&x,&y,&z);
for(int i=; i<=k; i++) //分为k+1层图,
{
edgeadd(x+i*n,y+i*n,z); //每层图之间建边
if(i!=k)
{
edgeadd(x+i*n,y+(i+)*n,);//第层i向i+1层图建边 边权为0;
}
}
}
dijikstra(st,ed);
}
}

BZOJ 2763

dp思想

/**************************************************************
Problem: 2763
User: 1071532391
Language: C++
Result: Accepted
Time:7452 ms
Memory:6972 kb
****************************************************************/ #include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define huan printf("\n");
#define debug(a,b) cout<<a<<" "<<b<<" ";
using namespace std;
const int maxn=2e4+,inf=0x3f3f3f3f;
typedef long long ll;
typedef pair<int,int> pii;
struct edge
{
int u,v,w,next;
}e[maxn*];
struct node
{
int x,y;
};
int cnt,dis[maxn][],head[maxn],vis[maxn][];
int n,m,k,s,t;
void init()
{
cnt=;
fillchar(head,-);
fillchar(vis,);
}
void addedge(int u,int v,int w)
{
e[++cnt].next=head[u];
e[cnt].u=u;
e[cnt].v=v;
e[cnt].w=w;
head[u]=cnt;
}
void spfa()
{
fillchar(dis,0x3f);
queue<node> q;
q.push(node{s,k});
dis[s][k]=;
while(!q.empty())
{
int u=q.front().x;
int t=q.front().y;
q.pop();
vis[u][t]=;
for(int i=head[u];i!=-;i=e[i].next)
{
int v=e[i].v,w=e[i].w;
if(dis[v][t]>dis[u][t]+w)
{
dis[v][t]=dis[u][t]+w;
if(!vis[v][t])
{
vis[v][t]=;
q.push(node{v,t});
}
}
if(t>&&dis[v][t-]>dis[u][t])
{
dis[v][t-]=dis[u][t];
if(!vis[v][t-])
{
vis[v][t-]=;
q.push(node{v,t-});
}
}
}
}
int ans=1e9;
for(int i=;i<=k;i++)
{
//cout<<dis[i][0]<<" "<<i<<endl;
ans=min(ans,dis[t][i]);
}
printf("%d\n",ans);
}
int main()
{
init();
scanf("%d%d%d%d%d",&n,&m,&k,&s,&t);
for(int i=;i<m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
spfa();
return ;
}

ACM-ICPC 2018 南京赛区网络预赛 L && BZOJ 2763 分层最短路的更多相关文章

  1. ACM-ICPC 2018 南京赛区网络预赛 L题(分层最短路)

    题目链接:https://nanti.jisuanke.com/t/31001 题目大意:给出一个含有n个点m条边的带权有向图,求1号顶点到n号顶点的最短路,可以使<=k条任意边的权值变为0. ...

  2. ACM-ICPC 2018 南京赛区网络预赛 L题(分层图,堆优化)

    题目链接: https://nanti.jisuanke.com/t/31001 超时代码: #include<bits/stdc++.h> using namespace std; # ...

  3. ACM-ICPC 2018 南京赛区网络预赛 L. Magical Girl Haze 最短路+分层图

    类似题解 There are NN cities in the country, and MM directional roads from uu to v(1\le u, v\le n)v(1≤u, ...

  4. ACM-ICPC 2018 南京赛区网络预赛 L. Magical Girl Haze

    262144K   There are NN cities in the country, and MM directional roads from uu to v(1\le u, v\le n)v ...

  5. ACM-ICPC 2018 南京赛区网络预赛 L 【分层图最短路】

    <题目链接> 题目大意: 有N个城市,这些城市之间有M条有向边,每条边有权值,能够选择K条边 边权置为0,求1到N的最短距离. 解题分析: 分层图最短路模板题,将该图看成 K+1 层图,然 ...

  6. ACM-ICPC 2018 南京赛区网络预赛 L.Magical Girl Haze(分层最短路)

    There are N cities in the country, and M directional roads from u to v(1≤u,v≤n). Every road has a di ...

  7. ACM-ICPC 2018 南京赛区网络预赛 - L Magical Girl Haze (分层迪杰斯特拉)

    题意:N个点,M条带权有向边,求可以免费K条边权值的情况下,从点1到点N的最短路. 分析:K<=10,用dist[i][j]表示从源点出发到点i,免费j条边的最小花费.在迪杰斯特拉的dfs过程中 ...

  8. 【ACM-ICPC 2018 南京赛区网络预赛 L】Magical Girl Haze

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 定义dis[i][j]表示到达i这个点. 用掉了j次去除边的机会的最短路. dis[1][0]= 0; 在写松弛条件的时候. 如果用 ...

  9. ACM-ICPC 2018 南京赛区网络预赛 L. Magical Girl Haze (分层dijkstra)

    There are NN cities in the country, and MMdirectional roads from uu to v(1\le u, v\le n)v(1≤u,v≤n). ...

随机推荐

  1. 《Python基础教程》 读书笔记 第五章(下)循环语句

    5.5.1while循环 x=1 while x<=100: print x x+=1 确保用户输入了名字: name="" while not name: name=raw ...

  2. .netcore中使用EFCore连接SQL Server并部署至Ubuntu

    前面一篇记录了如何在windows下开发asp.net core程序,并部署至ubuntu系统中.但仅仅是建立了一个demo项目,项目本身并没有实现多少功能.多数时候,我们的项目是要和数据库打交道.E ...

  3. poptip 外面 放 input 使用 iview vue

    外层套的是 <FormItem prop="name" label="姓名:"> <Input v-model="tFill.nam ...

  4. 和为S

    2518 和为S 2 秒 262,144 KB 10 分 2 级题   小b有一个01序列A,她想知道A有多少个非空连续子序列和为S. 你能帮帮她吗? 收起   输入 第一行输入一个数n,表示A的长度 ...

  5. Mathematics-基础:斐波那契数列

    f(1)=1 f(2)=1 f(n)=f(n-1)+f(n-2) n>2

  6. jqury 延迟方法

    $("button").click(function(){    $("#div1").delay("slow").fadeIn();    ...

  7. 智能指针unqiue_ptr

    unique_ptr unique_ptr 对它指向的对象在同一时刻是独占的.它要么在构造的时候使用内置指针初始化,要么使用reset给其赋值.当unique_ptr被销毁时,它所指向的对象也被销毁. ...

  8. [LOJ] 分块九题 3

    https://loj.ac/problem/6279 区间修改,区间查询前驱. TLE无数,我觉得这代码最精髓的就是block=1000. 谜一样的1000. 两个启示: 块内可以维护数据结构,比如 ...

  9. h5 页面 禁止网页缩放

    //禁用双指缩放: document.documentElement.addEventListener('touchstart', function (event) { if (event.touch ...

  10. go语言的碎片整理:time

    时间和日期是我们编程中经常用到的,本文主要介绍了Go语言内置的time包的基本用法. Go语言中导入包 单行导入 import "time" import "fmt&qu ...