这道题也算是一道模板题,但是第一次用优先队列迪杰斯特拉就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的更多相关文章

  1. poj 1511 Invitation Cards spfa 邻接矩阵

    题目链接: http://poj.org/problem?id=1511 题目大意: 这道题目比较难理解,我读了好长时间,最后还是在队友的帮助下理解了题意,大意就是,以一为起点,求从一到其他各点的最短 ...

  2. POJ 1511 最短路spfa

    题很简单 就是有向图中求给出的源点到其余所有点的最短路的和与其余所有点到源点的最短路之和 一开始以为dij对于正权图的单源最短路是最快的 写了一发邻接表的dij 结果超时 把所有的cin改成scanf ...

  3. Poj 1511 Invitation Cards(spfa)

    Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...

  4. POJ 1511 Invitation Cards (spfa的邻接表)

    Invitation Cards Time Limit : 16000/8000ms (Java/Other)   Memory Limit : 524288/262144K (Java/Other) ...

  5. poj 1511(spfa)

    ---恢复内容开始--- http://poj.org/problem?id=1511 一个spfa类的模板水题. 题意:就是求从1到n个点的来回的所有距离和. 对spfa类的题还是不太熟练,感觉还是 ...

  6. POJ 1511 Invitation Cards (最短路spfa)

    Invitation Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description In the age ...

  7. DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards

    题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...

  8. HDU 1535 Invitation Cards (POJ 1511)

    两次SPFA. 求 来 和 回 的最短路之和. 用Dijkstra+邻接矩阵确实好写+方便交换.可是这个有1000000个点.矩阵开不了. d1[]为 1~N 的最短路. 将全部边的 邻点 交换. d ...

  9. 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 / ...

随机推荐

  1. Redis分布式锁,基于StringRedisTemplate和基于Lettuce实现setNx

    使用redis分布式锁,来确保多个服务对共享数据操作的唯一性一般来说有StringRedisTemplate和RedisTemplate两种redis操作模板. 根据key-value的类型决定使用哪 ...

  2. Android笔记——Activity中的回传数据案例(装备选择)

    1.创建程序: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...

  3. POJ 3468 A Simple Problem with Integers(线段树功能:区间加减区间求和)

    题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

  4. Thinkphp中如何书写按照指定字段同步更新的ORM

    群友提出一个问题,如何在更新某个字段的时候同步另一个字段数据过来,即 update table set column1 =column2 where xxx 写原生SQL当然可行,不过既然有ORM那就 ...

  5. iOS开发之--使用storyboard下,tabbar小图标和文字颜色的设置

    在开发项目的时候,如果是使用故事版设计的架构,那么在设置tabbar小图标的时候,可能会出现一点小问题, 成功的设置方法如下: 1.设置seleectedImage和image,其实就是非选中状态的图 ...

  6. 微信开发工具包,jar包

    https://www.oschina.net/code/snippet_218887_22896 github地址:https://github.com/wuweiit/weixinapi

  7. android签名,制作key

    签名具体步骤: Apk签名首先要有一个keystore的签名用的文件. keystore是由jdk自带的工具keytool生成的.具体生成方式参考一下: 开始->运行->cmd->c ...

  8. web测试点--摘录

    转载地址:Web测试到底是在测什么 一.输入框 1.字符型输入框: (1)字符型输入框:英文全角.英文半角.数字.空或者空格.特殊字符“~!@#¥%……&*?[]{}”特别要注意单引号和&am ...

  9. Exchange Database Status(Copy Status ,Content Index State,QueueLength,Move Status...)

    Copy Status Description Mounted The active copy is online and accepting client connections. Only the ...

  10. ipconfig /flushdns

    C:\Users\sas>ipconfig /flushdns Windows IP 配置 已成功刷新 DNS 解析缓存. C:\Users\sas>ipconfig --help 错误: ...