题意:

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. TFS2010升级至TFS2013完全指南(更换服务器)

    一.背景:         公司已使用tfs2010很长时间,目前随着公司的发展,项目越来越少,而产品越来越多,采用的开发模式,也逐渐从瀑布式.迭代式转向敏捷开发.为了更好的支持产品研发,决定将tfs ...

  2. QTableWidget表头样式

    转载请注明出处:http://www.cnblogs.com/dachen408/p/7742680.html QTableView { background-color: rgba(255, 255 ...

  3. 纯css滚动公告栏目

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. [Github筆記] 清除所有 Commit 紀錄

    # 把原來的 git 移除掉 sudo rm .git -r # 初始化 git init git remote add origin https://github.com/username/repo ...

  5. java异常处理中的细节

    首先看一段代码 public class Test{ public static String output=""; public static void foo(int i){ ...

  6. python 字符与字节 json序列和反序列及支持的类型

    b = b"demo" s = "demo" # 字符串转字节 s = bytes(s, encoding = "utf8") s = st ...

  7. core 中使用 swagger

    引包 代码 public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddMvc().Set ...

  8. 【整理】iview Tree数据格式问题,无限递归树处理数据

    iview Tree数据格式问题,无限递归树处理数据 https://juejin.im/post/5b51a8a4e51d455d6825be20

  9. Swift语言Storyboard教程:第二部

    本文由CocoaChina翻译小组@TurtleFromMars翻译自raywenderlich,原文:Storyboards Tutorial in Swift: Part 2 更新记录:该Stor ...

  10. 服务器编程心得(四)—— 如何将socket设置为非阻塞模式

    1. windows平台上无论利用socket()函数还是WSASocket()函数创建的socket都是阻塞模式的: SOCKET WSAAPI socket( _In_ int af, _In_ ...