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 ...
随机推荐
- 解决win service 2003 IIS发布Gis网站后,访问地图服务出错,无法正常打开而且 事件查看器出现错误提示。
错误详情: 应用程序-特定 权限设置未将 COM 服务器应用程序(CLSID 为{379376DB-AEA6-40D1-9491-9345E61EF6BE})的 本地 激活 权限授予用户 NT AUT ...
- Android studio混淆
看了一篇关于Android studio混淆的文章http://blog.csdn.net/qq_23547831/article/details/51581491,感觉有必要总结一个简单的混淆版本设 ...
- VsSharp:一个VS扩展开发框架(上)
上篇:设计 一.引子 自2008年起开发SSMS插件SqlSharp(er)的过程中,有一天发现多数代码都大同小异,就像这样. Commands2 commands = (Commands2)_app ...
- PAT_2-08. 用扑克牌计算24点
一副扑克牌的每张牌表示一个数(J.Q.K分别表示11.12.13,两个司令都表示6).任取4张牌,即得到4个1~13的数,请添加运算符 (规定为加+ 减- 乘* 除/ 四种)使之成为一个运算式.每个数 ...
- java Web Services搭建环境时遇到的各种问题,记录一下。 java.lang.OutOfMemoryError: PermGen space,org/apache/struts2/util/ObjectFactoryDestroyable
情况:在同一个,myEclipes 下加载俩个项目,一个seriver端,一个client端. 必备: myEclipes ,apache-tomcat-7.0.42,apache-tomcat ...
- OC - 27.CATransition
概述 简介 CATransition又称转场动画,是CAAnimation的子类,可以直接使用 转场动画主要用于为图层提供移入/移出屏幕的动画效果 转场动画常见的应用是UINavigationCont ...
- ios9 http请求失败的问题
最近做项目的时候 将电脑版本升级到10.11.3 xcode'升级到 7.2 但是在模拟器上边进行数据请求的时候告诉我说网路哦有问题 截图如下 通过网络终于找到了解决的办法 原来是ios9 采用 ...
- 纯命令行教你Cocoapods的安装和使用
关于cocoapods的介绍和作用,网上有很多大神介绍的比我清楚,建议去看一下唐巧的http://blog.devtang.com/blog/2014/05/25/use-cocoapod-to-ma ...
- java中的继承要点
java的一大特性既是:继承. 1.因为有了一个子类继承了一个父类,才有了后面的多态. 2.类的继承,不要为了节省代码,为了继承而继承,把那个没有任何相关的类链接在一起,继承必须用在 is a,就是例 ...
- JS获取IP
新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js新浪多地域测试方法:http://int.dpool.s ...