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 / ...
随机推荐
- iOS开发之--NSData与UIImage之间的转换
//NSData转换为UIImage NSData *imageData = [NSData dataWithContentsOfFile: imagePath]; UIImage *image = ...
- iOS开发之 -- NSStringFromSelector的使用
很多时候,我们要触发一个时间,需要设置点击时间,当然了,有很多,比如:按钮,手势,tableview和其他一些空间自带的点击方法, 还有一个就是NSStringFromSelector的使用,废话不多 ...
- 各种层次NET人应该知道些什么?
A.任何一个使用.NET的人 1.描述线程与进程的区别? 2.什么是Windows服务,它的生命周期与标准的EXE程序有什么不同 ? 3.Windows上的单个进程所能访问的最大内存量是多少?它与系统 ...
- 利用脚手架vue cli搭建vue项目
vue.js https://vuejs.org/ 基础: http://cn.vuejs.org/v2/guide/installation.html 1.安装需要利用npm包管理器,所以首先安装n ...
- 使用pinyin4j实现汉字转拼音
1. maven项目,请在pom.xml里边添加包依赖相关配置: <dependency> <groupId>net.sourceforge.pinyin4j</grou ...
- Aggregate (GROUP BY) Function Descriptions
w Table 13.25 Aggregate (GROUP BY) Functions Name Description AVG() Return the average value of the ...
- container,algorith,iterate
\ http://morningspace.51.net/resource/stlintro/stlintro.html 标准容器 C++标准容器分为序列容器和关联容器,对于序列容器,C++提供的基本 ...
- Storm-源码分析- Component ,Executor ,Task之间关系
Component包含Executor(threads)的个数 在StormBase中的num-executors, 这对应于你写topology代码时, 为每个component指定的并发数(通过s ...
- ICO成本价
[当前ICO成本价]仅供参考:ICOcoin 成本价1元SNT 成本价0.26元UGT 成本价2.7元PAY 成本价6元OMG 成本价2.6元YOYO 成本价 0.17元BNT 成本价26元BAT 成 ...
- php中mysql_pconnect()的实现方式.
网上有人说要想让mysql_pconnect正常稳定的工作,必须保证mysql max_connect参数设定大于或等于apache的最大线程(进程)数.这句话是有一定道理的.这要简单了解mysql_ ...