ZOJ-2008-Invitation Cards(dijkstra)
题意:
在有向加权图中G(V,E),邮局要从起点S向其他n个节点发送邮件,于是派出n个邮递员,分别到达其他n个地点发送,然后回到起点S,求出所有邮递员所经过的总路程的最小值。
分析:
正向一次dijkstra,反向一次dijkstra
// File Name: 2008.cpp
// Author: Zlbing
// Created Time: 2013年08月04日 星期日 14时18分49秒 #include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
using namespace std;
#define CL(x,v); memset(x,v,sizeof(x));
#define INF 0x3f3f3f3f
#define LL long long
#define REP(i,r,n) for(int i=r;i<=n;i++)
#define RREP(i,n,r) for(int i=n;i>=r;i--)
const int MAXN=1e6+;
struct Edge{
int from,to,dist;
};
struct HeapNode{
int d,u;
bool operator <(const HeapNode &rhs)const{
return d>rhs.d;
}
};
vector<int> O_G[MAXN];
vector<Edge> O_edges;
struct Dijkstra{
int n,m;//点和边数
vector<Edge>edges;//边列表
vector<int>G[MAXN];//每个节点出发的边编号(从0开始编号)
bool done[MAXN];//是否永久标记
int d[MAXN];//s到各个点的距离
int p[MAXN];//最短路中的上一条边 void init(int n)
{
this->n=n;
for(int i=;i<=n;i++)//清空邻接表
{
G[i].clear();
}
edges.clear();//清空边表
}
void AddEdge(int from,int to,int dist)
{
//如果是无向图,每条无向边需调用2次AddEdge
edges.push_back((Edge){from,to,dist});
m=edges.size();
G[from].push_back(m-);
}
void dijkstra(int s)
{//求s到所有点的距离
priority_queue<HeapNode> Q;
for(int i=;i<=n;i++)d[i]=INF;
d[s]=;
memset(done,,sizeof(done));
Q.push((HeapNode){,s});
while(!Q.empty())
{
HeapNode x=Q.top();Q.pop();
int u=x.u;
if(done[u])continue;
done[u]=true;
for(int i=;i<G[u].size();i++)
{
Edge& e=edges[G[u][i]];
if(d[e.to]>d[u]+e.dist)
{
d[e.to]=d[u]+e.dist;
p[e.to]=G[u][i];
Q.push((HeapNode){d[e.to],e.to});
}
}
}
}
};
Dijkstra solver;
int main()
{
int N;
scanf("%d",&N);
while(N--)
{
int n,m;
scanf("%d%d",&n,&m);
solver.init(n);
int a,b,c;
for(int i=;i<=n;i++)
O_G[i].clear();
O_edges.clear();
for(int i=;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
solver.AddEdge(a,b,c);
O_edges.push_back((Edge){b,a,c});
int mm=O_edges.size()-;
O_G[b].push_back(mm);
}
solver.dijkstra();
int ans=;
for(int i=;i<=n;i++)
ans+=solver.d[i];
for(int i=;i<=n;i++)
solver.G[i]=O_G[i];
solver.edges=O_edges;
solver.dijkstra();
for(int i=;i<=n;i++)
ans+=solver.d[i];
printf("%d\n",ans);
}
return ;
}
ZOJ-2008-Invitation Cards(dijkstra)的更多相关文章
- 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优先队列)
题目链接:http://poj.org/problem?id=1511 就是求从起点到其他点的最短距离加上其他点到起点的最短距离的和 , 注意路是单向的. 因为点和边很多, 所以用dijkstra优先 ...
- POJ 1511 Invitation Cards(Dijkstra(优先队列)+SPFA(邻接表优化))
题目链接:http://poj.org/problem?id=1511 题目大意:给你n个点,m条边(1<=n<=m<=1e6),每条边长度不超过1e9.问你从起点到各个点以及从各个 ...
- HDU1535——Invitation Cards(最短路径:SPAF算法+dijkstra算法)
Invitation Cards DescriptionIn the age of television, not many people attend theater performances. A ...
- HDU 1535 Invitation Cards(逆向思维+邻接表+优先队列的Dijkstra算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1535 Problem Description In the age of television, n ...
- POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 16178 Accepted: 526 ...
- HDU 1535 Invitation Cards(最短路 spfa)
题目链接: 传送门 Invitation Cards Time Limit: 5000MS Memory Limit: 32768 K Description In the age of te ...
- poj1511/zoj2008 Invitation Cards(最短路模板题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Invitation Cards Time Limit: 5 Seconds ...
- POJ1511:Invitation Cards(最短路)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 34743 Accepted: 114 ...
- hdu1535——Invitation Cards
Invitation Cards Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
随机推荐
- 为什么我选择使用 Blocks(块)
扯淡:到了新公司接手新框架之后,发现大量的使用Blocks,之前很多时候都是使用代理,突然面对这个陌生的语法,特地科普总结了一番. 什么是Blocks 一句话概括就是,带有局部变量的匿名函数(即不带名 ...
- 高效 css 整理
避免通用规则 请确保规则不以通用类型作为结束! 不要用标签名或 classes 来限制 ID 规则 如果规则的关键选择器为 ID 选择器,则没有必要为规则增加标签名.因为 ID 是唯一的,增加标签只会 ...
- 设计模式学习——准备(UML类图)
前言 其实吧,最早接触UML是源于软件设计师的考试,半路出家实在难为我了.学设计模式总是要画类图的,所以补充UML的类图的知识是很重要滴.第一篇就偷懒一点copy别人的东西了.实话说,我们都是踩在巨人 ...
- WPF Binding值转换器ValueConverter使用简介(二)-IMultiValueConverter
注: 需要继承IMultiValueConverter接口,接口使用和IValueConverter逻辑相同. 一.MultiBinding+Converter 多值绑定及多值转换实例 当纵向流量大于 ...
- js 表单不为空,数字长度验证
$(document).ready(function() { //聚焦第一个输入框 $("#invtype").focus(); ...
- CI 笔记2,(命令规范等)
调试模式开启,$this->output->enable_profiler(TRUE); 保留字,不能和控制器重名,有3个,CI_Controller ,Default, index.这三 ...
- JavaScript Boolean(布尔) 对象
创建 Boolean 对象 Boolean 对象代表两个值:"true" 或者 "false" 下面的代码定义了一个名为 myBoolean 的布尔对象: va ...
- BFC块级格式化上下文简述
做过页面编写的各位应该对定位不陌生了,这个样式表中的重头戏,也是最难把控的元素之一,今天在这里我们要讲到的就是与浮动与清除浮动相关的定位元素,对于定位有很多种,有绝对定位,还有相对定位,固定定位,静态 ...
- Java Swing 使用总结(转载)
随笔转载自:此去经年ぢ 地址:http://www.cnblogs.com/FLFL/p/5369756.html 1. GUI编程引言 以前的学习当中,我们都使用的是命令交互方式: 例如:在 ...
- Ubuntu14.04安装AMD显卡驱动双屏显示器完全解决方案
网上有很多方法,但是针对AMD显卡的方案不多,所以笔者今天想写一篇关于AMD显卡的教程. 首先,进入这个网址下载一些东东:http://support.amd.com/zh-cn/download/d ...