bzoj1576[Usaco2009 Jan]安全路径Travel(堆优化dijkstra+并查集)
来源:互联网 发布:淘宝商品被删除怎么办 编辑:程序博客网 时间:2024/06/07 07:12
spfa会被卡。。
思想和3694差不多。
就是最短路树要自己跑dij建出来,其余的基本上一模一样。
#include<cstdio>#include<algorithm>#include<cstring>#include<iostream>#include<queue>#define fo(i,a,b) for(int i=a;i<=b;i++)#define fd(i,a,b) for(int i=a;i>=b;i--)using namespace std;const int N=5e5+5;int n,m;int tot,head[N],next[N],go[N],val[N],f[N],cnt;int fa[N],bel[N],dis[N],h[N];int vis[N],re[N];struct node{ int x,y,l,len;}edge[N],e[N];struct p{ int pt,v; bool operator < (const p &x)const { return v>x.v; }};priority_queue<p>q;inline void add(int x,int y,int z){ go[++tot]=y; val[tot]=z; next[tot]=head[x]; head[x]=tot;}inline void dij(){ memset(dis,0x7f,sizeof(dis)); dis[1]=0; q.push((p){1,0}); while (!q.empty()) { p now=q.top(); q.pop(); int x=now.pt; for(int i=head[x];i;i=next[i]) { int v=go[i]; if (dis[v]>dis[x]+val[i]) { dis[v]=dis[x]+val[i]; h[v]=h[x]+1; re[v]=i; fa[v]=x; q.push((p){v,dis[v]}); } } }}inline int find(int x){ if (x==f[x])return x; else return f[x]=find(f[x]);}inline bool cmp(node a,node b){ return a.len<b.len;}int main(){ scanf("%d%d",&n,&m); fo(i,1,m) { int x,y,z; scanf("%d%d%d",&x,&y,&z); add(x,y,z); add(y,x,z); e[i].x=x,e[i].y=y,e[i].l=z; } dij(); fo(i,2,n)vis[re[i]]=1; fo(i,1,m) if (!vis[i*2-1]&&!vis[i*2]) { edge[++cnt].x=e[i].x; edge[cnt].y=e[i].y; edge[cnt].l=e[i].l; } fo(i,1,cnt) edge[i].len=edge[i].l+dis[edge[i].x]+dis[edge[i].y]; sort(edge+1,edge+1+cnt,cmp); fo(i,1,n)f[i]=i; fo(i,1,cnt) { int x=edge[i].x,y=edge[i].y; int fx=find(x),fy=find(y); int lastx=0,lasty=0; while (fx!=fy) { if (h[fx]<h[fy])swap(x,y),swap(fx,fy),swap(lastx,lasty); if (!bel[x]) { bel[x]=i; if (lastx)f[lastx]=x; } else if (lastx)f[lastx]=x; lastx=fx; x=fa[lastx]; fx=find(x); } } fo(i,2,n) if (bel[i])printf("%d\n",edge[bel[i]].len-dis[i]); else printf("-1\n");}
0 0
- [BZOJ1576][Usaco2009 Jan]安全路径Travel(堆优化dijkstra+并查集)
- bzoj1576[Usaco2009 Jan]安全路径Travel(堆优化dijkstra+并查集)
- bzoj 1576: [Usaco2009 Jan]安全路经Travel(dijkstra堆优化+并查集)
- 【最短路径树+可并堆/树链剖分】BZOJ1576 [Usaco2009 Jan]安全路经Travel
- [Usaco2009 Jan]安全路经Travel dijkstra + 并查集
- 【bzoj1576】[Usaco2009 Jan]安全路经Travel
- BZOJ1576: [Usaco2009 Jan]安全路经Travel
- [bzoj1576] [Usaco2009 Jan]安全路经Travel
- [BZOJ1576] [Usaco2009 Jan]安全路经Travel
- [Usaco2009 Jan]安全路经Travel(最短路树+并查集/树链剖分)
- BZOJ1576——[Usaco2009 Jan]安全路经Travel
- BZOJ1576 洛谷P2934 : [Usaco2009 Jan]安全路经Travel
- 【bzoj1576】【安全路径Travel】【dijkstra+树链剖分】
- [BZOJ1576]安全路径travel-最短路径树+并查集
- [删边最短路 并查集] BZOJ 1576 [Usaco2009 Jan]安全路经Travel
- 【BZOJ1576】[Usaco2009 Jan]安全路经Travel【最短路树】【树链剖分】【线段树】
- 【BZOJ 1576】 [Usaco2009 Jan]安全路经Travel
- bzoj 1576 [Usaco2009 Jan]安全路经Travel
- 网络仿真软件的对比
- React同构直出优化总结
- 线性回归的来历
- Android框架之路——OkGo的使用
- 使用Wrapper创建java的window系统服务
- bzoj1576[Usaco2009 Jan]安全路径Travel(堆优化dijkstra+并查集)
- IOS使用OpenAL播放音频文件
- 【Qt Multimedia】用QMediaPlayer实现简易播放器(2)详解QMediaPlayer、QVideoWidget、QMediaPlaylist
- PDF与Word工具以及翻译网站推荐
- 530 Error: A secure connection is requiered(such as ssl).
- [LeetCode]136. Single Number
- HDU 4804 Campus Design(状压DP)
- 学生成绩管理系统(转载)
- POJ NOI0105-40 数1的个数