题目链接:Invitation Cards

题意:

  给出一张有向图,现在要求从1到其他所有的结点的最小路径和与从所有其他结点到1的最小路径和之和。

题解:

  求最小路径可以用SPFA来求解。从1到其他结点的正向跑一遍SPFA就可以求出来了,要求其他结点到1的最小路径则可以反向建图跑一边SPFA。但是这里面很奇怪的是,如果正向和反向建图开两个vector就会TLE,开一个就不会TLE了。@。@?

 #include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
typedef pair<int,int> P;
struct edge{
int u,v,val;
}; const int MAX_N = 1e6+;
const int INF = 1e9+;
vector<P> vec[MAX_N];
vector<P> vec2[MAX_N];
int res[MAX_N];
int vis[MAX_N];
edge tran[MAX_N];
queue<int> que;
int N,M,T,a,b,x;
long long ans;
void init()
{
for(int i=;i<=N;i++)
{
vec[i].clear();
vec2[i].clear();
}
while(!que.empty()) que.pop();
ans = ;
}
void spfa(int z)
{
while(!que.empty()) que.pop();
for(int i=;i<=N;i++)
{
res[i] = INF;
vis[i] = ;
}
res[z] = ;
vis[z] = ;
que.push(z);
while(!que.empty())
{
int t = que.front();
que.pop();
vis[t] = ;
for(int i=;i<vec[t].size();i++)
{
P temp = vec[t][i]; if(res[temp.first] > res[t] + temp.second)
{
res[temp.first] = res[t] + temp.second;
if(vis[temp.first] == )
{
vis[temp.first] = ;
que.push(temp.first);
}
} }
}
for(int i=;i<=N;i++) ans += res[i];
} int main()
{
cin>>T;
while(T--)
{
scanf("%d%d",&N,&M);
init();
for(int i=;i<M;i++)
{
scanf("%d%d%d",&tran[i].u,&tran[i].v,&tran[i].val);
vec[tran[i].u].push_back(P(tran[i].v,tran[i].val));
}
spfa();
for(int i=;i<=N;i++) vec[i].clear();
for(int i=;i<M;i++)
{
vec[tran[i].v].push_back(P(tran[i].u,tran[i].val));
}
spfa();
printf("%lld\n",ans);
}
return ;
}

Invitation Cards POJ-1511 (spfa)的更多相关文章

  1. Invitation Cards POJ 1511 SPFA || dij + heap

    http://poj.org/problem?id=1511 求解从1去其他顶点的最短距离之和. 加上其他顶点到1的最短距离之和. 边是单向的. 第一种很容易,直接一个最短路, 然后第二个,需要把边反 ...

  2. Invitation Cards POJ - 1511 (双向单源最短路)

    In the age of television, not many people attend theater performances. Antique Comedians of Malidine ...

  3. (最短路 SPFA)Invitation Cards -- poj -- 1511

    链接: http://poj.org/problem?id=1511 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82829#probl ...

  4. Invitation Cards POJ - 1511

    题目链接:https://vjudge.net/problem/POJ-1511 思路:题目意思就是,从1出发到所有城市,再从所有城市回到1的最短时间. 那么我们只要正跑一次图,然后反向存边,再跑一次 ...

  5. POJ1511 Invitation Cards —— 最短路spfa

    题目链接:http://poj.org/problem?id=1511 Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Tota ...

  6. POJ 1511 Invitation Cards(Dijkstra(优先队列)+SPFA(邻接表优化))

    题目链接:http://poj.org/problem?id=1511 题目大意:给你n个点,m条边(1<=n<=m<=1e6),每条边长度不超过1e9.问你从起点到各个点以及从各个 ...

  7. POJ 1511 SPFA+邻接表 Invitation Cards

    题目大意: 计算从 1 点 到 其他所有点的 往返距离之和, 因为是 有向图, 所以我们需要将图反存 一次, 然后求两次单源最短路, 结果就出来了. #include <iostream> ...

  8. poj 1511(spfa)

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

  9. poj 1511(SPFA+邻接表)

    题目链接:http://poj.org/problem?id=1511 思路:题目意思很简单就是要求源点到各点的最短路之和,然后再求各点到源点的最短路之和,其实就是建两个图就ok了,其中一个建反图.1 ...

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

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

随机推荐

  1. 转: ASP.NET MVC 多语言配置

    步骤1:打开VS2015新建测试项目. 步骤2:创建资源文件 App_GlobalResources下.    Resource1.resx    Resource1.zh-cn.resx   步骤3 ...

  2. Linux 中 FQDN 查询及设置

    FQDN:(Fully Qualified Domain Name)全限定域名:同时带有主机名和域名的名称 其实就是标注一个主机的完整域名.比如我的域名为 ifrom.top 那么它的邮件服务器的主机 ...

  3. round()和trunc()用法

    round(数字 | 列 保留小数的位数):四舍五入. select a.*,round(s),round(-s) from bqh4 a trunc(数字 | 列 保留小数的位数):舍弃指定位置的内 ...

  4. SDN 第二次作业

    问题 1.为什么需要SDN?SDN特点? 答:当今网络快速发展,用户的需求也就日益增加,但网络的创新速度却并没有增加,而是比较缓慢.传统网络中的网络设备是硬件.操作系统.网络应用紧耦合的,每个设备厂商 ...

  5. [python] 在 python2和3中关于类继承的 super方法简要说明

    下面举一个例子,同样的代码使用 python2 和 python3 写的,大家注意两段程序中红色加粗的部分: python2的类继承使用super方法: #-*- coding:utf-8 -*- ' ...

  6. vultr vps(ubuntu)忘记密码

    参考官方解决方案:https://www.vultr.com/docs/boot-into-single-user-mode-reset-root-password 在此仅给出ubuntu下的解决 D ...

  7. Mapreduce运行过程分析(基于Hadoop2.4)——(二)

    4.3 Map类    创建Map类和map函数.map函数是org.apache.hadoop.mapreduce.Mapper类中的定义的,当处理每一个键值对的时候,都要调用一次map方法,用户须 ...

  8. java中Integer与int装箱拆箱一点收获

    示例代码: class BoxIntInteger { public static void main(String[] args) { Integer a = new Integer(10111); ...

  9. Thinkphp5.0分页和跳页

    后台查询商品或者会员量需要用到分页展示列表,当页数比较多的时候为了体高用户体验度,需要添加一个跳页也就是手动输入页码数进行快速跳转指定页面.由于手动编写分页比较麻烦,又想使用TP5自带的分页,但是TP ...

  10. pyspider爬取数据存入redis--2.测试数据库连通性

    直接上代码 #!/usr/bin/env python # -*- encoding: utf-8 -*- # Created on 2017-10-27 09:56:50 # Project: re ...