题意:

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. Javaweb学习笔记6—EL表达式与JSTL及自定义标签

    今天来讲javaweb的第六阶段学习. EL表达式与JSTL及自定义标签是对上篇文章介绍的JSP的扩展,不能说是很重要的东西,但是也要了解. 老规矩,首先先用一张思维导图来展现今天的博客内容. ps: ...

  2. codevs 1519 过路费

    时间限制: 1 s  空间限制: 256000 KB  题目等级 : 大师 Master 题目描述 Description 在某个遥远的国家里,有 n个城市.编号为 1,2,3,…,n.这个国家的政府 ...

  3. Android(java)学习笔记184:多媒体之 MediaPlayer使用

    MediaPlayer类可用于控制音频/视频文件或流的播放.关于如何使用这个类的方法还可以阅读VideoView类的文档. 1.MediaPlayer 状态图       对播放音频/视频文件和流的控 ...

  4. docker 新手入门 (阿里镜像仓库的使用)

    创建镜像仓库后的步骤是:   https://help.aliyun.com/document_detail/60743.html?spm=a2c4g.11186623.6.546.79be52f3y ...

  5. 联玛客(T 面试)

    我看你写的项目都是SSM架构,那我们就来聊下Spring 1.Spring的生命周期,与生命周期相关的事件? 2.阿里巴巴开发手册中的规范有哪些? 切到了异常捕捉话题 3.线程你有了解吗? 创建线程的 ...

  6. 制作JPEGImages出现的bug

    我用的是下面这个脚本进行改名字: import os import sys path = "/home/bnrc/py-faster-rcnn/data/VOCdevkit2007/VOC2 ...

  7. Moebius for SQLServer负载均衡

    搞数据库的都知道:在Oracle上有RAC集群,MySQL也有对应的方案,而SQL Server上直到SQL Server 2012版本的AlwaysOn到来,微软都没有提供一个负载均衡方案,在网上看 ...

  8. NodeJs运行服务器-day01

    //读取内置模块http,这个模块开发服务器用的var http =require('http'); var server=http.createServer(function(req,res){ r ...

  9. luogu P2734 游戏 A Game

    https://www.luogu.org/problemnew/show/P2734 数据范围比较小,二位DP可做,而luogu 3004,虽然几乎一模一样(只是数据范围大点),则需要压维. 定义f ...

  10. MySQL丨03丨基本语句

    MySQL语句都是以 ; 号结尾的 看库(刘大婶直接面对的是各种档案袋) show databases; 建库(新弄了一个档案袋) create database database_name; 删库( ...