当前位置:首页 >> 跨学科知识体系 >> 【SPFA】重建道路(jzoj 1212),thl w8

【SPFA】重建道路(jzoj 1212),thl w8

cpugpu芯片开发光刻机 跨学科知识体系 1
文件名:【SPFA】重建道路(jzoj 1212),thl w8 【SPFA】重建道路(jzoj 1212) 重建道路 jzoj 1212 题目大意:

有一个图,其中的一些路坏了,为了从a走到b,问最少修复多长的路

输入样例 321 2 12 3 211 21 3 输出样例 1 数据范围

2⩽N⩽1002\leqslant N\leqslant 1002N100 N−1⩽D⩽M⩽N∗(N−1)/2N-1\leqslant D\leqslant M\leqslant N*(N-1)/2N1DMN(N1)/2 1⩽x,y⩽n,x≠y1\leqslant x,y\leqslant n,x\neq y1x,yn,x=y 0<len<1000< len <1000<len<100

解题思路:

把坏掉的边的权值保留,其他边改为0,然后直接spfa跑一遍就可以了

代码: #include<queue>#include<cstdio>#include<cstring>#include<iostream>using namespace std;int n,m,k,x,y,z,h,tot,p[150],b[150],head[150],pp[150][150];struct rec{int to,next,l,lon;}a[20500];void spfa()//spfa模板{scanf("%d%d",&x,&y);memset(b,127/3,sizeof(b));queue<int>d;d.push(x);p[x]=1;b[x]=0;while(!d.empty()){h=d.front();d.pop();for (int i=head[h];i;i=a[i].next)if (b[h]+a[i].l<b[a[i].to]){b[a[i].to]=b[h]+a[i].l;if(!p[a[i].to]){p[a[i].to]=1;d.push(a[i].to);}}p[h]=0;}printf("%d",b[y]);return;}int main(){scanf("%d%d",&n,&m);for (int i=1;i<=m;++i){scanf("%d%d%d",&x,&y,&z);//连边a[++tot].to=y;a[tot].lon=z;a[tot].next=head[x];head[x]=tot;pp[x][y]=tot;//记下来a[++tot].to=x;a[tot].lon=z;a[tot].next=head[y];head[y]=tot;pp[y][x]=tot;}scanf("%d",&k);for (int i=1;i<=k;++i){scanf("%d%d",&x,&y);a[pp[x][y]].l=a[pp[x][y]].lon;//赋值a[pp[y][x]].l=a[pp[y][x]].lon;}spfa();return 0;}
协助本站SEO优化一下,谢谢!
关键词不能为空
同类推荐
«    2025年12月    »
1234567
891011121314
15161718192021
22232425262728
293031
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
文章归档
网站收藏
友情链接