POJ 1511 - Invitation Cards (dijkstra优先队列)
题目链接: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优先队列)的更多相关文章
- POJ 1511 Invitation Cards(Dijkstra(优先队列)+SPFA(邻接表优化))
题目链接:http://poj.org/problem?id=1511 题目大意:给你n个点,m条边(1<=n<=m<=1e6),每条边长度不超过1e9.问你从起点到各个点以及从各个 ...
- 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 / ...
- POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 16178 Accepted: 526 ...
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
- [POJ] 1511 Invitation Cards
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 18198 Accepted: 596 ...
- POJ 1511 Invitation Cards (spfa的邻接表)
Invitation Cards Time Limit : 16000/8000ms (Java/Other) Memory Limit : 524288/262144K (Java/Other) ...
- POJ 1511 Invitation Cards (最短路spfa)
Invitation Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description In the age ...
- Poj 1511 Invitation Cards(spfa)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...
- (简单) POJ 1511 Invitation Cards,SPFA。
Description In the age of television, not many people attend theater performances. Antique Comedians ...
随机推荐
- bzoj1297: [SCOI2009]迷路
矩阵. 一个图的邻接矩阵的m次幂相当于 长度恰好为m的路径数.这要求边权为1. 因为边权小于等于9,所以可以把一个点拆成9的点. 拆成的第(i+1)个点向第i个点连边. 如果存在边(u,v,w) 就由 ...
- IE6、IE7、IE8中overflow:hidden无效问题
在做图片无缝滚动效果时遇到了这个兼容问题 div宽1000px高250px超出隐藏. 但在Firefox中正常,超出部分隐藏,但是在IE6.IE7.IE8.Sogou高速下都显示了出来.做了这么多年的 ...
- android中的ellipsize设置(省略号的问题)
textview中有个内容过长加省略号的属性,即ellipsize,可以较偷懒地解决这个问题,哈哈~ 用法如下: 在xml中 android:ellipsize = "end" ...
- codevs 1218 疫情控制
啊好烦这道题.... 基本思路网上都有. 注意的一点是在匹配的时候,如果有军队的来源没有被匹配到,那么就先匹配这个来源.(因为不花钱). 不过数据好水.... #include<iostream ...
- macro names must be identifiers
1.错把 #include 写成了 #define 会报这个错 2.定义一个不存在的宏业会报这个错,如加了-DANDRO 而ANDRO不存在
- I.MX6 Android netperf
/***************************************************************************** * I.MX6 Android netpe ...
- jquery仿邮箱文本输入框自动加载邮箱后缀
在像百度这样的网站注册时,你会看到输入邮箱会出现自动给用户输入补全主流邮箱.这种对于增加用户体验的小例子已司空见惯.正好看到人家写的这种js功能.还挺不错,使用起来很方便,几乎不用写神呢代码.&quo ...
- mysql 外连接总结
内连接: 只连接匹配的行左外连接: 包含左边表的全部行(不管右边的表中是否存在与它们匹配的行),以及右边表中全部匹配的行右外连接: 包含右边表的全部行(不管左边的表中是否存在与它们匹配的行),以及左边 ...
- Binary Tree Level Order Traversal java实现
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- Long Dominoes(ZOJ 2563状压dp)
题意:n*m方格用1*3的方格填充(不能重叠)求有多少种填充方法 分析:先想状态,但想来想去就是觉得不能覆盖所有情况,隔了一天,看看题解,原来要用三进制 0 表示横着放或竖放的最后一行,1表示竖放的中 ...