题目链接:

  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

poj 1511 Invitation Cards spfa 邻接矩阵的更多相关文章

  1. POJ 1511 Invitation Cards (spfa的邻接表)

    Invitation Cards Time Limit : 16000/8000ms (Java/Other)   Memory Limit : 524288/262144K (Java/Other) ...

  2. Poj 1511 Invitation Cards(spfa)

    Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...

  3. POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)

    POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...

  4. SPFA算法(2) POJ 1511 Invitation Cards

    原题: Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 31230   Accepted: ...

  5. POJ 1511 Invitation Cards (最短路spfa)

    Invitation Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description In the age ...

  6. (简单) POJ 1511 Invitation Cards,SPFA。

    Description In the age of television, not many people attend theater performances. Antique Comedians ...

  7. POJ 1511 Invitation Cards 链式前向星+spfa+反向建边

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 27200   Accepted: 902 ...

  8. POJ 1511 Invitation Cards(逆向思维 SPFA)

    Description In the age of television, not many people attend theater performances. Antique Comedians ...

  9. [POJ] 1511 Invitation Cards

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 18198   Accepted: 596 ...

随机推荐

  1. MySQL入门笔记 - 视图

    参考书籍<MySQL入门很简单> 1.视图定义 视图是从一个或者多个表中导出来的虚拟的表,透过这个窗口可以看到系统专门提供的数据,使用户可以只关心对自己有用的数据,方便用户对数据操作,同时 ...

  2. centos Crontab

    minute   hour   day   month   week   command     顺序:分 时 日 月 周 命令 第1列分钟1-59第2列小时1-23(0表示子夜)第3列日1-31第4 ...

  3. Leetcode:search_insert_position

    一.     题目 给定一个数组和要插入数的大小.求插入的位置. 二.     分析 太水,直接扫描.过--. class Solution { public: int searchInsert(in ...

  4. Codeforces 344B Simple Molecules

    #include<bits/stdc++.h> using namespace std; int main() { int a,b,c; scanf("%d%d%d", ...

  5. maven 项目 spring mvc + jdbc 配置文件

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  6. 基于docker容器搭建fastdfs分布式文件系统

    本次环境的搭建参考了 https://blog.csdn.net/qq_43455410/article/details/84797814, 感谢博主. 主要流程如下: 1. 下载fastdfs镜像 ...

  7. thinkphp3.2.3 数据库写入add 方法的一些问题。

    最近在做项目中遇到的一个数据操作add()方法,在不开启debug的模式下会漏掉一些字段没写入数据库. 当时并不知道是这个原因,明明在开发的时候都是没问题的,怎么突然出现这个问题,找了好久都没有头绪, ...

  8. Android数据与服务器交互的GET,POST,HTTPGET,HTTPPOST的使用

    Android有这几种方式,可以提交数据到服务器,他们是怎么使用的呢,这里我们来探讨一下. 这里的例子用的都是提交客户端的用户名及密码,同时本节用到的StreamTools.readInputStre ...

  9. The input stream is not a valid binary format.

    The input stream is not a valid binary format. The starting contents (in bytes) are: 53-79-73-74-65- ...

  10. YTU 2439: C++习题 复数类--重载运算符+

    2439: C++习题 复数类--重载运算符+ 时间限制: 1 Sec  内存限制: 128 MB 提交: 1022  解决: 669 题目描述 定义一个复数类Complex,重载运算符"+ ...