题目链接: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. 【three.js练习程序】创建简单物理地形

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. Oracle EBS INV 释放保留

    CREATE or REPPLACE PROCEDURE RelieveReservation AS -- Common Declarations l_api_version NUMBER := 1. ...

  3. Oracle EBS AR应收核销取值

    AR_RECEIVABLE_APPLICATIONS APP, AR_CASH_RECEIPTS CR, AR_PAYMENT_SCHEDULES PS_INV, HZ_CUST_ACCOUNTS C ...

  4. EJB Remote/Local 绑定和JNDI Lookup

    从同事那里学到一种方便的注解SessionBean的方式.代码我放到github去了 https://github.com/EdisonXu/Test/commit/703d49123dca9e666 ...

  5. C# socket 发送图片和文件

    先说服务端:界面:如图: 界面设计源码 namespace SocketJPGToTxt { partial class Form1 { /// <summary> /// 必需的设计器变 ...

  6. Automation Script For Percona Xtrabackup FULL/Incremental

    This is my first post in 2019, and Im starting with a MySQL solution. In MySQL world, implementing a ...

  7. 小米正式开源 SQL 智能优化与改写工具 SOAR

    近日,小米正式宣布开源 SOAR. 截至今日,该项目已经获得了 350 个「star」以及 44 个「fork」(GitHub项目地址:https://github.com/XiaoMi/soar) ...

  8. Interpreting /proc/meminfo and free output for Red Hat Enterprise Linux 5, 6 and 7

    Interpreting /proc/meminfo and free output for Red Hat Enterprise Linux 5, 6 and 7 Solution Verified ...

  9. MySQL面试之说明myisam和innodb两种存储引擎的不同之处

    1.事务的支持不同(innodb支持事务,myisam不支持事务) 2.锁粒度(innodb行锁应用,myisam表锁) 3.存储空间(innodb既缓存索引文件又缓存数据文件,myisam只能缓存索 ...

  10. MySQL基础之 索引

    MySQL索引讲解 索引的好处: MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度. 打个比方,如果合理的设计且使用索引的MySQL是一辆兰博基尼的话,那么 ...