题目链接:https://vjudge.net/problem/POJ-1511

思路:题目意思就是,从1出发到所有城市,再从所有城市回到1的最短时间。

那么我们只要正跑一次图,然后反向存边,再跑一次图,把所有单源最短路相加就是答案了。

emmm,这题,很卡时间,作为一个懒人,用c++的输入输出,加了简单的快读,用了链式前项星,

还是险些通过,感觉遇到很大的输入数据,用c++时,最好减少空间申请和回收的开销,不然会卡。

我一个优先队列在函数申请用,T了,在全局申请,再清空就A了。。。当然,这是之前的,后面改了下,

emm,7780ms。。。当然,这题用dijkstra一定要堆优化一下,点有1e6之多。。。


 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <string>
#include <map>
#include <cmath>
#include <iomanip>
using namespace std; typedef long long LL;
#define inf 1e10
#define rep(i,j,k) for(int i = (j); i <= (k); i++)
#define rep__(i,j,k) for(int i = (j); i < (k); i++)
#define per(i,j,k) for(int i = (j); i >= (k); i--)
#define per__(i,j,k) for(int i = (j); i > (k); i--) const int N = 1e6 + ;
int head[N];
int vis[N];
int dis[N];
int U[N],V[N],W[N];
int cnt;
LL ans; int n,m; struct Edge{
int to;
int w;
int next;
}e[N]; struct node{
int loc;
int w; bool friend operator< (const node& a,const node& b){
return a.w > b.w;
}
};
priority_queue<node > que; void add(int u,int v,int w){
e[cnt].to = v;
e[cnt].w = w;
e[cnt].next = head[u];
head[u] = cnt++;
} void dijkstra(){ while(!que.empty()) que.pop();
rep(i,,n) vis[i] = false;
rep(i,,n) dis[i] = inf;
dis[] = ;
que.push(node{,}); while(!que.empty()){
int u = que.top().loc;
que.pop();
if(vis[u]) continue;
vis[u] = true; for(int o = head[u]; ~o; o = e[o].next){
int v = e[o].to;
int w = e[o].w; if(!vis[v] && dis[v] > dis[u] + w){
dis[v] = dis[u] + w;
que.push(node{v,dis[v]});
}
}
}
rep(i,,n) ans += dis[i];
} int main(){ ios::sync_with_stdio(false);
cin.tie(); int T;
cin >> T;
while(T--){
cin >> n >> m; rep(i,,n) head[i] = -;
cnt = ; int u,v,w;
rep(i,,m){
cin >> U[i] >> V[i] >> W[i];
add(U[i],V[i],W[i]); //正向存边
} ans = ;
dijkstra(); rep(i,,n) head[i] = -;
cnt = ;
rep(i,,m){
add(V[i],U[i],W[i]); //反向存边
} dijkstra();
cout << ans << endl;
} getchar(); getchar();
return ;
}

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

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

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

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

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

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

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

  4. [POJ] 1511 Invitation Cards

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 18198   Accepted: 596 ...

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

  6. POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 16178   Accepted: 526 ...

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

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

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

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

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

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

随机推荐

  1. oracle 循环插入数据

    参考链接:oracle 行转列 pivot函数基本用法 --建表 --drop table SalesList; create table SalesList( keHu varchar2(20), ...

  2. 《深度学习》圣经"花书"经验法则中文版!

    作者:Jeff Macaluso https://jeffmacaluso.github.io/post/DeepLearningRulesOfThumb/ 转自CVer,仅用作个人学习 当我在研究生 ...

  3. JDOJ3011 铺地板III

    JDOJ3011 铺地板III https://neooj.com/oldoj/problem.php?id=3011 题目描述 有3 x N (0 <= N <= 105)的网格,需要用 ...

  4. http与Rpc

    RPC即远程服务调用 出现原因:随着项目越来越大,访问量越来越大,为了突破性能瓶颈,需要将项目拆分成多个部分,这样比起传统的项目都是本地内存调用,分布式的项目之间需要在网络间进行通信 服务之间的远程调 ...

  5. bootstrap-editable 中关于onEditableSave 回调

    问题描述 在对bootstrap-editable 进行编辑时,有两种使用方法:一种直接在每一个column中进行编辑保存,例如:{ title:'标题', field:'title', width: ...

  6. centos配置ADSL拨号 配置阿里云的yum源

    如果系统yum源有问题可以更改yum源配置阿里云的yum源1)下载repo文件 wget http://mirrors.aliyun.com/repo/Centos-7.repo(没有 wget命令可 ...

  7. linux操作系统 - 综合习题

    登录超级用户,完成以下操作: [linux@slave053 ~]$ su - 1.用户和组群管理(本大题共5小题,共10分) (1)创建两个用户tangseng,monkey,并指定密码为12345 ...

  8. 数据仓库001 - 复习Linux shell命令 - pwd mkdir mv tail -f xxx.log 和 ail -F xxx.log

    1.  [root@localhost ~]#  的含义 ?  [登录的用户  机器的名称 家目录] 2. 查看当前光标所在的目录   pwd [root@localhost ~]# pwd /roo ...

  9. 解决WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!

    问题: 当我想要利用win10本地的cmd进行: ssh root@192.168.1.230 时,出现了如下错误: C:\Users\Raodi>ssh root@192.168.1.230 ...

  10. win7 下docker 镜像加速

    打开 Kitematic 运行 docker cli 注册镜像 https://www.daocloud.io/mirror#accelerator-doc 上有镜像地址 sudo sed -i &q ...