题目链接:http://poj.org/problem?id=1511

就是求从起点到其他点的最短距离加上其他点到起点的最短距离的和 , 注意路是单向的.

因为点和边很多, 所以用dijkstra优先队列的做法.

起点到其他点的最短距离之和就是dij一下 . 要求其他点到起点的最短距离的话就是把原先边的方向反向一下,然后再求起点到其他点的最短距离之和 , 同样dij一下.

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue> using namespace std;
const int MAXN = 1e6 + ;
typedef long long LL;
typedef pair <LL , LL> P;
vector <P> edge[MAXN]; //这里邻接表二元组(pair)的second代表边的终点标号, first代表边的距离
bool ok[MAXN];
LL d[MAXN] , INF = 1e12;
int a[MAXN] , b[MAXN] , c[MAXN]; //分别代表边的起点 终点 权值 void init(int n) {
for(int i = ; i <= n ; i++) {
d[i] = INF;
ok[i] = false;
edge[i].clear();
}
} void dijkstra(int s) {
d[s] = ;
priority_queue <P , vector<P> , greater<P> > Q; //优先队列 二元组的first:最短距离 second:点的标号
Q.push(P( , s)); //压入起点
while(!Q.empty()) {
P p = Q.top();
Q.pop();
int v = p.second;
if(ok[v]) {
continue;
}
ok[v] = true;
for(int i = ; i < edge[v].size() ; i++) {
P temp = edge[v][i];
if(d[temp.second] > d[v] + temp.first) {
d[temp.second] = d[v] + temp.first;
Q.push(P(d[temp.second] , temp.second));
}
}
}
} int main()
{
int t , n , m;
LL u , v , cost;
scanf("%d" , &t);
while(t--) {
scanf("%d %d" , &n , &m);
init(n);
for(int i = ; i < m ; i++) {
scanf("%d %d %d" , &a[i] , &b[i] , &c[i]);
edge[a[i]].push_back(P(LL(c[i]) , LL(b[i])));
}
LL res = ;
dijkstra();
for(int i = ; i <= n ; i++) {
res += d[i];
}
init(n);
for(int i = ; i < m ; i++) {
edge[b[i]].push_back(P(LL(c[i]) , LL(a[i]))); //每条边反向一下
}
dijkstra();
for(int i = ; i <= n ; i++) {
res += d[i];
}
printf("%lld\n" , res);
}
}

POJ 1511 - Invitation Cards (dijkstra优先队列)的更多相关文章

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

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

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

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

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

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

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

  5. [POJ] 1511 Invitation Cards

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

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

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

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

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

  8. Poj 1511 Invitation Cards(spfa)

    Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...

  9. (简单) POJ 1511 Invitation Cards,SPFA。

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

随机推荐

  1. Huge CSV and XML Files in Python, Error: field larger than field limit (131072)

    Huge CSV and XML Files in Python January 22, 2009. Filed under python twitter facebook pinterest lin ...

  2. 1128. Partition into Groups(图着色bfs)

    1128 写的dfs貌似不太对 bfs重写 用bfs将图进行黑白染色 如果有超过一个与自己颜色相同的点 就把该点存入栈中 最后处理栈中的点 判断此点是否合法 不合法 取反 取反后再判断相邻点是否合法 ...

  3. 为PHP增加PDO-Mysql驱动

    一.问题 公司有一台老的Linux服务器,Apache+MySQL+Php结构的, 要把最近做的一个PHP项目部署到上面,做为测试环境, 由于新项目是用PHP的YII框架开发的,而YII框架的数据访问 ...

  4. linux 服务自动调用

    php服务地址: http://192.168.2.117/web/index.php/buy/auctionselect 编写脚本service.sh,内容: #! /bin/sh crul htt ...

  5. POJ 3259 Wormholes 虫洞(负权最短路,负环)

    题意: 给一个混合图,求判断是否有负环的存在,若有,输出YES,否则NO.有重边. 思路: 这是spfa的功能范围.一个点入队列超过n次就是有负环了.因为是混合图,所以当你跑一次spfa时发现没有负环 ...

  6. shell 的判断与比较

    1  shell 的$! ,$?, $$,$@ $n        $1 the first parameter,$2 the second... $#        The number of co ...

  7. 最简单的视音频播放示例7:SDL2播放RGB/YUV

    本文记录SDL播放视频的技术.在这里使用的版本是SDL2.实际上SDL本身并不提供视音频播放的功能,它只是封装了视音频播放的底层API.在Windows平台下,SDL封装了Direct3D这类的API ...

  8. CImage 获取图片RGB 、图片高和宽;

    1 CImage img , img1 ,imDest; 2 img1.Load( 图片路径); 3 img.Load( 图片路径); 4 为了防止图片失真,先处理一下在把图片显示出来 5 SetSt ...

  9. 【DFS+记忆搜索】NYOJ-10-Skiing

    [题目链接:NYOJ-10] skiing 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑 ...

  10. 【DFS深搜初步】HDOJ-2952 Counting Sheep、NYOJ-27 水池数目

    [题目链接:HDOJ-2952] Counting Sheep Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...