题目链接:

  http://poj.org/problem?id=1511

题目大意:

  这道题目比较难理解,我读了好长时间,最后还是在队友的帮助下理解了题意,大意就是,以一为起点,求从一到其他各点的最短回路总和。

解题思路:

  解决这个题目有几个容易错的,解决了离ac就不远了^_^。

  1:数据范围是1<=边数<=顶点数<=1000000,所以不能用邻接矩阵,要用邻接表,用vector实现时要动态申请内存。

  2:求得是起始点到其他点的最短回路和,我们可以建两个邻接表(一个正向,一个负向邻接表),对两个表分别spfa就可以了。

  3:数据太大,最后结果要用__int64或者long long保存。

(这是第一次写spfa,也是第一次用vector实现邻接表,在队友的帮助下一直从大早上到现在终于解决了,可以松口气去吃饭了)

 #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 1000005
#define INF 2000000000 struct Egde
{
int e, w;
Egde(int e=, int w=) : e(e),w(w) {};//构造函数,初始化
};
bool vis[maxn];
__int64 dist[maxn], p;
vector< vector<Egde> >G[]; void init ()
{
int i;
for (i=; i<=p; i++)
dist[i] = INF;
}
void spfa (int x, int s); int main ()
{
int t, q;
scanf ("%d", &t);
while (t --)
{
scanf ("%d %d", &p, &q);
G[].clear();
G[].resize(p+);
G[].clear();
G[].resize(p+); for (int i=; i<q; i++)
{
int s, e, w;
scanf ("%d %d %d", &s, &e, &w);
G[][s].push_back (Egde(e, w));
G[][e].push_back (Egde(s, w));
} __int64 sum = ;
spfa (, );
for (int i=; i<=p; i++)
sum += dist[i];
spfa (, );
for (int i=; i<=p; i++)
sum += dist[i];
printf ("%I64d\n", sum); }
return ;
} void spfa (int x, int s)
{
Egde pn;
queue<Egde>que;
memset (vis, false, sizeof(vis));
init();
pn.e = s, pn.w = ;
dist[s] = ;
que.push (pn);
vis[pn.e] = true;
while (!que.empty())
{
pn = que.front();
que.pop();
vis[pn.e] = false;
int len = G[x][pn.e].size();
for (int i=; i<len; i++)
{
Egde p = G[x][pn.e][i];
if (dist[p.e] > dist[pn.e] + p.w)
{
dist[p.e] = dist[pn.e] + p.w;
if (!vis[p.e])
{
vis[p.e] = true;
que.push(p);
}
}
}
}
}

相识类型:poj2387

最新文章

  1. LCIS
  2. H5项目常见问题及注意事项
  3. 跟我学Windows Azure 一 创建Windows Azure试用账号
  4. scala基础语法(变量,数据类型,函数)
  5. 初探设计:Java接口和抽象类何时用?怎么用?
  6. webform添加到webapi的支持
  7. css3 转换transfrom 过渡transition 和两个@
  8. Supermarket_贪心
  9. 使用 as 和 is 运算符安全地进行强制转换
  10. UVaLive 6809 Spokes Wheel (模拟)
  11. UVA 10557 XYZZY
  12. Android studio dabao
  13. org.hibernate.PropertyNotFoundException: Could not find a getter for employee in class com.itcast.f_hbm_oneToMany.Department
  14. 【转】Entity Framework 5.0系列之自动生成Code First代码
  15. 谨慎修改Python的类属性
  16. app后端设计(5)-- 表情的处理
  17. 一)surging 微服务框架使用系列之surging 的准备工作rabbitmq安装(转载 https://www.cnblogs.com/alangur/p/8339905.html)
  18. plw的晚餐(毒瘤题害我暴0)
  19. python之tkinter使用-滚动条
  20. df

热门文章

  1. Android 自己定义UI文章汇总
  2. History(历史)命令用法 15 例
  3. Java中常见的排序算法
  4. 【Mongodb教程 第一课 】 MongoDB下载安装
  5. 迅雷CTO李金波:致创业者的一封信
  6. PHP记录商品历史纪录
  7. HDFS的体系架构
  8. HDU 2457/POJ 3691 DNA repair AC自动机+DP
  9. Calling a parent window function from an iframe
  10. zTree 基本用法