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 ...
随机推荐
- CentOS5.5 正式开始安装 Oracle 11g r2(图形界面安装)
一.下载oracle 官方网站, 可以下载最新版本 Oracle Database 11g Release http://www.oracle.com/index.html CentOS5. i386 ...
- Jqgrid入门-结合Struts2+json实现数据展示(五)
DEMO用的是ssh框架实现的,具体怎么搭建的就不多做说明了.分页表格的数据操作难点就是数据展现.至于增删改直接用hibernate原生的方法实现即可. 初步分析:表格要实现分页,那么 ...
- Facebook存储技术方案:找出“暖性BLOB”数据
Facebook公司已经在其近线存储体系当中彻底弃用RAID与复制机制,转而采用分布式擦除编码以隔离其所谓的“暖性BLOB”. 暖性?BLOB?这都是些什么东西?大家别急,马上为您讲解: BLOB—— ...
- android-async-http
安装 http://blog.csdn.net/wangwei_cq/article/details/9453345 包内的一些基本的参数 http://www.cnblogs.com/manuose ...
- iphone 如何清空UIWebView的缓存
iphonecachingapplicationcookiescacheperformance I actually think it may retain cached information ...
- [反汇编练习] 160个CrackMe之016
[反汇编练习] 160个CrackMe之016. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...
- jsoup使用选择器语法来查找元素
问题 你想使用类似于CSS或jQuery的语法来查找和操作元素. 方法 可以使用Element.select(String selector) 和 Elements.select(String sel ...
- 如何打开和关闭Oracle Flashback
1.打开flashback: 关闭数据库 SQL>shutdown immediate; 启动到mount方式 SQL>startup mount; 如果归档没有打开,打开归档[因为fla ...
- mybatis Mapper XML 文件
MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会立即发现省掉了将近 95% ...
- 按钮点击WIN8 磁贴效果
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...