POJ - 1511 - 两次SPFA
这道题也算是一道模板题,但是第一次用优先队列迪杰斯特拉就T了。1e6的数据量,给了8s,网上其他题解中说要用SPFA。
题意:N个点的带权有向图。每次都从1出发,要到达其余没有被访问过的一个点(发传单?),然后返回,过程中其余被访问的点不计算在内。求整个过程走过的最短路程。
分析:用原图跑SPFA计算从源点1到其余各点的最短路,再将原图转置为反向图,对反向图再跑一遍SPFA,计算出各点到1的最短路(求各点到一个点的最短路是这么个操作)。
然后求sigma(d[i]+rd[i])。
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<map>
#include<string>
#include<algorithm>
#include<queue>
//#define LOCAL
using namespace std;
typedef long long LL;
const LL INF =(1ll<<);
const int maxn =1e6+; struct Edge{
int to,next;
LL val;
}; struct SPFA{
int head[maxn];
Edge edges[maxn];
LL d[maxn];
bool inq[maxn];
int n,tot; void init(int n){
this->tot=;
this->n = n;
memset(head,-,sizeof(head));
}
void AddEdge(int u,int v,LL val){
edges[tot].to = v;
edges[tot].val = val;
edges[tot].next = head[u];
head[u] = tot++;
} void spfa(int s){
for(int i=;i<=n;++i){
inq[i]=false;
d[i] = INF;
}
queue<int> Q;
Q.push(s);
d[s]=; inq[s] = true;
while(!Q.empty()){
int x = Q.front();Q.pop();
inq[x] =false;
for(int i = head[x];~i;i=edges[i].next){
int v = edges[i].to;
if(d[v]>d[x]+edges[i].val){
d[v] = d[x]+edges[i].val;
if(!inq[v]){
Q.push(v);
inq[v] = true;
}
}
}
}
}
}G,rG; int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int N,M,u,v;
LL tmp;
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&N,&M);
G.init(N);
rG.init(N);
for(int i=;i<=M;++i){
scanf("%d%d%lld",&u,&v,&tmp);
G.AddEdge(u,v,tmp);
rG.AddEdge(v,u,tmp);
}
G.spfa();
rG.spfa();
LL res=;
for(int i=;i<=N;++i){
res+=G.d[i]+rG.d[i];
}
printf("%lld\n",res);
}
return ;
}
POJ - 1511 - 两次SPFA的更多相关文章
- poj 1511 Invitation Cards spfa 邻接矩阵
题目链接: http://poj.org/problem?id=1511 题目大意: 这道题目比较难理解,我读了好长时间,最后还是在队友的帮助下理解了题意,大意就是,以一为起点,求从一到其他各点的最短 ...
- POJ 1511 最短路spfa
题很简单 就是有向图中求给出的源点到其余所有点的最短路的和与其余所有点到源点的最短路之和 一开始以为dij对于正权图的单源最短路是最快的 写了一发邻接表的dij 结果超时 把所有的cin改成scanf ...
- Poj 1511 Invitation Cards(spfa)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...
- POJ 1511 Invitation Cards (spfa的邻接表)
Invitation Cards Time Limit : 16000/8000ms (Java/Other) Memory Limit : 524288/262144K (Java/Other) ...
- poj 1511(spfa)
---恢复内容开始--- http://poj.org/problem?id=1511 一个spfa类的模板水题. 题意:就是求从1到n个点的来回的所有距离和. 对spfa类的题还是不太熟练,感觉还是 ...
- POJ 1511 Invitation Cards (最短路spfa)
Invitation Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description In the age ...
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
- HDU 1535 Invitation Cards (POJ 1511)
两次SPFA. 求 来 和 回 的最短路之和. 用Dijkstra+邻接矩阵确实好写+方便交换.可是这个有1000000个点.矩阵开不了. d1[]为 1~N 的最短路. 将全部边的 邻点 交换. d ...
- POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)
POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...
随机推荐
- WPF DataGrid DataGridTemplateColumn 控制模板中控件
<DataGrid Name="DG"> <DataGrid.Columns> < ...
- 浅谈AutoResetEvent的用法
using System;using System.Threading; namespace AutoResetEvent_Examples{ class MyMainClass { ...
- php队列算法[转]
<?php/*** php队列算法* * Create On 2010-6-4* Author Been* QQ:281443751* Email:binbin1129@126.com**/cl ...
- 多线程下的神奇的IOCP
https://blog.csdn.net/lijia626482312/article/details/40858061 一个人从接到项目到昨天终于完成,用了差不多4个月,其中各种心酸和眼泪.我的项 ...
- 怎样使用DWZ?
首先说明,这篇文章不是解说DWZ内部实现原理的,也不打算分析它的源代码,这里仅仅是演示一下,怎样将DWZ框架整合到项目中去. 刚刚过去的项目中,前台UI使用的是DWZ.因为之前项目的开发环境都已经搭建 ...
- MySQL------存储过程的使用
如图: 1.创建存储过程 create procudure userAdd(na varchar(20),pass varchar(20)) select * from user where name ...
- Ubuntu执行su后输入密码结果认证失败--解决办法:sudo passwd修改命令
- android硬件返回
1.第一种 @Override public boolean onKeyUp(int keyCode, KeyEvent event) { //点击回退键 if(Ke ...
- C语言条件运算符
如果希望获得两个数中最大的一个,可以使用 if 语句,例如: if(a>b){ max = a; }else{ max = b; } 不过,C语言提供了一种更加简单的方法,叫做条件运算符,语法格 ...
- 【BZOJ4071】[Apio2015]巴邻旁之桥 Treap
[BZOJ4071][Apio2015]巴邻旁之桥 Description 一条东西走向的穆西河将巴邻旁市一分为二,分割成了区域 A 和区域 B. 每一块区域沿着河岸都建了恰好 1000000001 ...