题意:

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. AI学习一:环境安装

    对于Python开发用户来讲,PIP安装软件包是家常便饭.但国外的源下载速度实在太慢,浪费时间.而且经常出现下载后安装出错问题.所以把PIP安装源替换成国内镜像,可以大幅提升下载速度,还可以提高安装成 ...

  2. 【HEVC帧间预测论文】P1.9 Coding Tree Depth Estimation for Complexity Reduction of HEVC

    Coding Tree Depth Estimation for Complexity Reduction of HEVC <HEVC标准介绍.HEVC帧间预测论文笔记>系列博客,目录见: ...

  3. Socket是什么呢?中间软件抽象层

    代表着网络连接 Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族隐藏在Socket接口后面,对用 ...

  4. dos命令及HTML基础语法

  5. 【简●解】[HAOI2007] 理想的正方形

    [简●解][HAOI2007] 理想的正方形 可恶的\(DP\). [题目大意] 有一个\(a*b\)的整数组成的矩阵,现请你从中找出一个\(n*n\)的正方形区域,使得该区域所有数中的最大值和最小值 ...

  6. Docker安装Oracle12C,导入dmp文件出现ORA-12170错误

    oracle版本为 sath89/oracle-12c oracle基本信息 hostname: localhost port: 1521 sid: xe username: system passw ...

  7. 2. CHARACTER_SETS

    2. CHARACTER_SETS CHARACTER_SETS表提供有关可用字符集的信息. 下表中的SHOW Name值对应于SHOW CHARACTER SET语句的列名. INFORMATION ...

  8. Python:字体颜色

    /033[31;1m     /033[0m 字体背景颜色 40:黑 41:深红 42:绿 43:黄色 44:蓝色 45:紫色 46:深绿 47:白色 字体颜色 30:黑 31:红 32:绿 33:黄 ...

  9. Django之学员管理二

    Django之学员管理二 学生表的一对多的增删改查 views.py def students(request): #select students.sid,students.name,classes ...

  10. 有关OEP脱壳

    首先补充: OEP:(Original Entry Point),程序的入口点,软件加壳就是隐藏了OEP(或者用了假的OEP), 只要我们找到程序真正的OEP,就可以立刻脱壳. PUSHAD (压栈) ...