题目链接: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. 初学JavaScript正则表达式(七)

    量词 例: \d{20}\w\d?\w+\d*\d{3}\w{3,5}\d{3,} 20次数字字符 单词字符 出现零次或一次数字字符 至少出现一次单词字符 出现任意次数字字符 出现3次数字字符 出现3 ...

  2. luoguP3017Brownie Slicing

    https://www.luogu.org/problem/P3017 题意 给你一个蛋糕,R行C列 ,每个点有巧克力碎屑(如下) 1 2 2 1 3 1 1 1 2 0 1 3 1 1 1 1 1 ...

  3. Apache(基于端口号)

    1.配置服务器的IP地址 2.  创建网站数据目录 (1).分别创建端口为6111,6222的网站数据目录 (2).分别在网站数据目录中写入不同的内容 3.在配置文件中描述基于端口号的虚拟主机 (1) ...

  4. Ant风格路径表达式

    ANT通配符有三种: ? 匹配任何单字符 * 匹配0或者任意数量的字符 ** 匹配0或者更多的目录 举例: /project/*.a 匹配项目根路径下所有在project路径下的.a文件 /proje ...

  5. 【day05】php

    一.时间日期函数库 1.安装:时间日期函数库PHPCORE组成部分 2.  (1)date_default_timezone_set(string $timezone)          设置时区  ...

  6. 一文读懂前端技术演进:盘点Web前端20年的技术变迁史

    本文原文由作者“司徒正美”发布于公众号“前端你别闹”,即时通讯网收录时有改动,感谢原作者的分享. 1.引言 1990 年,第一个Web浏览器的诞生:1991 年,WWW诞生,这标志着前端技术的开始. ...

  7. not in和not exists区别

    如果查询语句使用了not in 那么内外表都进行全表扫描,没有用到索引: 而not extsts 的子查询依然能用到表上的索引. 所以无论那个表大,用not exists都比not in要快. 也就是 ...

  8. webpack-实用的2个配置

    ①:运行npm run dev 后自动打开浏览器    首先找到config里面的index.js文件,然后打开找到autoOpenBrowser属性,把默认的false改为true即可 ②:简写路径 ...

  9. matplotlib画图相关

    一. plt显示一副图像 1. import matplotlib.pyplot as plt 2. plt.figure()                    # 图像名称 3. plt.ims ...

  10. mybaties报错:There is no getter for property named 'table' in 'class java.lang.String'

    报错是由于xml里获取不到这个table参数 package com.xxx.mapper; import java.util.List; import org.apache.ibatis.annot ...