POJ 1511 【heap+dij】
题意:
t组样例。
每组有n个节点,有m条单向边。
有m组输入,每组a b c 表示从a到b的单向边的权值是c。
求解,从编号为1的节点出发,有n-1个人,要求他们分别到达编号从2到n的节点再返回,所有边的权值的和最小是多少。
思路:
构图,构两个图,分别是正向图和反向图,然后用dij算单源最短路,将所有点到1的最短路加起来就是答案。
这题注意
1.inf原先的99999999定义不够大。
2.需要用到优先队列优化。
3.vector会超时,需要写手工邻接表。
4.一开始看到网上有人写getint();这样的函数发现在这道题并不能缩短时间,直接用scanf时间是1600,用了getint();时间是3200.
/*************************************************************************
> File Name: B.cpp
> Author: ttpond
> Created Time: 2015-8-18 16:47:3
************************************************************************/
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<set>
using namespace std;
int ednum;
struct st
{
int id,dis;
st(int a,int b)
{
id=a;
dis=b;
}
st(){};
};
struct cmp
{
bool operator()(const st &a,const st &b)
{
return a.dis>b.dis;
}
};
struct edge
{
int id,w;
edge *next;
};
edge *adj[][];
edge edges[][];
int dis[];
inline void addEdge(int a,int b,int c)
{
edge *aa,*bb;
aa=&edges[][ednum];
bb=&edges[][ednum];
ednum++;
aa->id=b;
aa->w=c;
aa->next=adj[][a];
adj[][a]=aa;
bb->id=a;
bb->w=c;
bb->next=adj[][b];
adj[][b]=bb;
}
int n,m;
const int inf=;
int dis1[];
void dfs(int num)
{
for(int i=;i<=n;i++)
{
dis[i]=inf;
}
priority_queue<st,vector<st>,cmp>q;
st tmp;
dis[]=;
q.push(st(,));
while(!q.empty())
{
tmp=q.top();
q.pop();
for(edge *p=adj[num][tmp.id];p;p=p->next)
{
if(dis[p->id]>p->w+dis[tmp.id])
{
dis[p->id]=p->w+dis[tmp.id];
q.push(st(p->id,dis[p->id]));
}
}
}
}int main()
{
int t,tt;
scanf("%d",&t);
int a,b,c,d;
//cout<<inf;
for(tt=; tt<t; tt++)
{
ednum=;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
for(int j=;j<;j++)
{
adj[j][i]=NULL;
}
}
for(int i=; i<m; i++)
{
scanf("%d%d%d",&a,&b,&c);
addEdge(a,b,c);
}
long long ans=;
for(int i=;i<;i++)
{
dfs(i);
for(int i=;i<=n;i++)
{
ans+=dis[i];
}
}
printf("%I64d\n",ans);
}
}
POJ 1511 【heap+dij】的更多相关文章
- poj 3225 【线段树】
poj 3225 这题是用线段树解决区间问题,看了两天多,算是理解一点了. Description LogLoader, Inc. is a company specialized in provid ...
- Girls and Boys POJ - 1466 【(二分图最大独立集)】
Problem DescriptionIn the second year of the university somebody started a study on the romantic rel ...
- POJ 1511 Invitation Cards dij
分析:正向加边,反向加边,然后两遍dij #include<cstdio> #include<cstring> #include<queue> #include&l ...
- POJ 1018 【枚举+剪枝】.cpp
题意: 给出n个工厂的产品参数带宽b和价格p,在这n个工厂里分别选1件产品共n件,使B/P最小,其中B表示n件产品中最小的b值,P表示n件产品p值的和. 输入 iCase n 表示iCase个样例n个 ...
- Building a Space Station POJ 2031 【最小生成树 prim】
http://poj.org/problem?id=2031 Description You are a member of the space station engineering team, a ...
- poj 2342 【Anniversary party】树形dp
题目传送门//res tp poj 题意 给出一棵有权树,求一个节点集的权值和,满足集合内的任意两点不存在边 分析 每个点有选中与不选中两种状态,对于第\(i\)个点,记选中为\(sel_i\),不选 ...
- poj 3280【区间dp】
poj 3280 题意:给定一个字符串和每个字符删去和增加的代价,求使字符串变成回文串操作所需的最小代价. 题解:哇!开心!终于亲自做对了!做完这两题这个就回了.uva10739 uva 10453 ...
- 括号序列问题 uva 1626 poj 1141【区间dp】
首先考虑下面的问题:Code[VS] 3657 我们用以下规则定义一个合法的括号序列: (1)空序列是合法的 (2)假如S是一个合法的序列,则 (S) 和[S]都是合法的 (3)假如A 和 B 都是合 ...
- poj 1701【数学几何】
The area Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
随机推荐
- PMP项目管理学习笔记(5)——整合管理之制定项目章程
关于两个输入 在很多过程中,会用到这两个输入: 企业环境要素 是关于你的公司如何开展业务所需要知道的所有信息. 在你计划项目时,有很多关于公司的信息会非常有用,你需要知道各个不同部门是如何运作的,你所 ...
- 跟随鼠标指针跑的div拖拽效果
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- itop安装中使用nginx安装后不能出现enter itop的问题
安装中没有出现enter itop,  如下的网络请求给了我们原因 原来使用的是域名请求资源文件,而该域名并不能指向我的服务器,所以安装中资源文件请求不成功,查看了我的nginx配置,如下 [ro ...
- docker新手入门(基本命令以及介绍)
Docker 的核心内容 镜像 (Image) 容器 (Container) 仓库 (Repository) Registry 用来保存用户构建的镜像 docker的开始使用: 1. docker ...
- window Chrome 下允许跨域访问服务端接口设置
关闭chrome,使用cmd命令进入chrome安装目录cd C:\Program Files (x86)\Google\Chrome\Application 然后使用命令打开chromechrome ...
- 第1节 flume:10、flume的更多组件介绍
作业:flume如何实现收集mysql的数据,没隔几秒钟,查看mysql中的数据是否有变化,一旦有变化,把数据拿过来,存到hdfs上. 需要使用custom source.可网上搜索,github上.
- 配置maven报错 the java_home environment variable is not defined correctly ......
the java_home environment variable is not defined correctly This environment variable is needed to r ...
- Maven实战读书笔记(四):Maven生命周期与插件
Maven的生命周期是对所有构建过程的抽象和统一.包含了项目的清理.初始化.编译.测试.打包.集成测试.验证.部署和站点生成等几乎所有构建步骤. Maven的生命周期是抽象的,其实际行为是由插件来完成 ...
- Spring AOP 详细介绍
一.理解 “面向切面编程” 面向切面编程(Aspect Oriented Programming,AOP)是软件编程思想发展到一定阶段的产物,是对面向对象编程(Object Oriented Prog ...
- curl 模拟post请求
curl -H "Content-Type:application/json" -X POST --data '{"openId":"xxxxxxx& ...