题目链接: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. LeetCode题解之 Binary Tree Preorder Traversal

    1.题目描述 2.问题分析 利用递归. 3.代码 vector<int> preorderTraversal(TreeNode* root) { vector<int> v; ...

  2. ORACLE-SQL微妙之处

    本文总结一下平时经常使用的SQL语句以及一些ORACLE函数的微妙之处.欢迎大家多多补充平时最常用的SQL语句,供大家学习参考. SQL> select * from temp2; NAME S ...

  3. 2. DAS,NAS,SAN在数据库存储上的应用

    一. 硬盘接口类型1. 并行接口还是串行接口(1) 并行接口,指的是并行传输的接口,比如有0~9十个数字,用10条传输线,那么每根线只需要传输一位数字,即可完成.从理论上看,并行传输效率很高,但是由于 ...

  4. It’s Time To Think Linq

    动机 如果你有以下迷惑,你应该看看这篇文章 你想办法找到所有与GameObject.FindGameObjectsWithTag的变换(),而不是游戏本身的对象 你需要操作,排序和更改列表和数组的类型 ...

  5. Use Jupyter notebook on Fedora 28

    生产环境使用 Fedora 28, 并且需要搭建一个 Jupyter 的notebook 方便使用,所搭建的Jupyter 支持单人远程 密码访问 1. 安装 安装 Jupyter , 出错 [roo ...

  6. selenium-百度搜索框输入后,定位联想下拉框元素

    1.输入关键字后,显示联想下拉框,鼠标右键对应的联想字段,点击检查,就可在F12模式下元素查看器中定位到,之后使用Xpath定位.

  7. vc获取当前进程CPU使用率

    double GetCPUUserRate() { HANDLE hProcess=::GetCurrentProcess(); static DWORD s_dwTickCountOld = 0; ...

  8. yaml格式

    yaml中允许表示三种格式,分别为常量值.对象和数组 例如: 其中#作为注释,yaml中只有行注释 基本格式要求: 1.大小写敏感:2.使用缩进代表层级关系: 3.缩进只能使用空格,不能使用tab键, ...

  9. 让sublime text3支持Vue语法高亮显示[转]

    1.准备语法高亮插件vue-syntax-highlight. 下载地址:https://github.com/vuejs/vue-syntax-highlight 下载页面并下载: 解开压缩包vue ...

  10. 线程相关代码分析->常见面试题(一、Thead类)

    As always,我们直接看jdk的代码切入: 首先是最简单的Runnable接口: public interface Runnable { public abstract void run(); ...