题意:

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】的更多相关文章

  1. poj 3225 【线段树】

    poj 3225 这题是用线段树解决区间问题,看了两天多,算是理解一点了. Description LogLoader, Inc. is a company specialized in provid ...

  2. Girls and Boys POJ - 1466 【(二分图最大独立集)】

    Problem DescriptionIn the second year of the university somebody started a study on the romantic rel ...

  3. POJ 1511 Invitation Cards dij

    分析:正向加边,反向加边,然后两遍dij #include<cstdio> #include<cstring> #include<queue> #include&l ...

  4. POJ 1018 【枚举+剪枝】.cpp

    题意: 给出n个工厂的产品参数带宽b和价格p,在这n个工厂里分别选1件产品共n件,使B/P最小,其中B表示n件产品中最小的b值,P表示n件产品p值的和. 输入 iCase n 表示iCase个样例n个 ...

  5. 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 ...

  6. poj 2342 【Anniversary party】树形dp

    题目传送门//res tp poj 题意 给出一棵有权树,求一个节点集的权值和,满足集合内的任意两点不存在边 分析 每个点有选中与不选中两种状态,对于第\(i\)个点,记选中为\(sel_i\),不选 ...

  7. poj 3280【区间dp】

    poj 3280 题意:给定一个字符串和每个字符删去和增加的代价,求使字符串变成回文串操作所需的最小代价. 题解:哇!开心!终于亲自做对了!做完这两题这个就回了.uva10739  uva 10453 ...

  8. 括号序列问题 uva 1626 poj 1141【区间dp】

    首先考虑下面的问题:Code[VS] 3657 我们用以下规则定义一个合法的括号序列: (1)空序列是合法的 (2)假如S是一个合法的序列,则 (S) 和[S]都是合法的 (3)假如A 和 B 都是合 ...

  9. poj 1701【数学几何】

    The area Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

随机推荐

  1. Mac上安装Homebrew和wget

    实际上是使用Homebrew来安装wget 安装Homebrew Homebrew一般称为brew,是Mac OSX上的软件包管理工具,能在Mac中方便的安装软件或者卸载软件, 只需要一个命令, 非常 ...

  2. 十个 JDBC 的最佳实践

    JDBC是Java为多种关系型数据库提供的统一的访问接口,以下是我长期使用JDBC总结的十个最佳实践. 1. 使用PrearedStatement 任何一个使用过JDBC的Java程序员几乎都知道这个 ...

  3. 洛谷 P1918 保龄球

    题目描述 DL 算缘分算得很烦闷,所以常常到体育馆去打保龄球解闷.因为他保龄球已经打了几十年了,所以技术上不成问题,于是他就想玩点新花招. DL 的视力真的很不错,竟然能够数清楚在他前方十米左右每个位 ...

  4. es 集群部署

    下载 [root@localhost ~]# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.1 ...

  5. 通过洛谷P2639看01背包

    题目描述 Bessie像她的诸多姊妹一样,因为从Farmer John的草地吃了太多美味的草而长出了太多的赘肉.所以FJ将她置于一个及其严格的节食计划之中.她每天不能吃多过H ( <= H &l ...

  6. mysql alter修改数据库表结构用法

    1.alter操作表字段 (1)增加字段 alter table 表名 add 字段名 字段类型: alter table student add name varchar(10): (2)修改字段 ...

  7. python中的参数、全局变量及局部变量

    1.位置参数.关键字参数.默认参数的使用 位置参数.关键字参数 def test(x,y,z): print(x) print(y) print(z) test(1,2,3) #位置参数,必须一一对应 ...

  8. 利用RestTemplate进行http调用

    在对接API的时候,会涉及调用第三方的服务,这时候可以利用RestTemplate进行调用,下面给大家展示一个简单的调用demo. package com.tanlu.user.api.control ...

  9. 从多表连接后的select count(*)看待SQL优化

    从多表连接后的select count(*)看待SQL优化 一朋友问我,以下这SQL能直接改写成select count(*) from a吗? SELECT COUNT(*) FROM a LEFT ...

  10. bash实现自动补全

    yum install -y bash-completion source /usr/share/bash-completion/bash_completion 执行后yum拥有选项自动补全功能 对于 ...