题目链接:

  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. Desert King (poj 2728 最优比率生成树 0-1分数规划)

    Language: Default Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 22113   A ...

  3. HTML的DIV如何实现垂直居中

    外部的DIV必须有如下代码 display:table-cell; vertical-align:middle;   这样可以保证里面的东西,无论是DIV还是文本都可以垂直居中

  4. apache下配置认证用户

    有时候我们须要给我apacheserver下制定的文件夹加上用户认证,方便一些而用户进行文件的浏览.配置例如以下: 1 设置用户 1 htpasswd -c file_path user_name 回 ...

  5. iOS 开发者中的个人账号与组织账号之间区别

    苹果对开发者主要分为3类:个人.组织(公司.企业).教育机构.即: 1.个人(Individual) 2.组织(Organizations) 组织类又分为2个小类: (1)公司(Company) (2 ...

  6. Unity构造函数注入代码示例

    Unity构造函数注入代码示例 如果使用 Unity 实例化一个类,该类的构造函数依赖一个或多个其他类,则 Unity 会为构造函数自动创建参数中指定的被依赖的类的实例.例如,下面的代码展示了一个名为 ...

  7. vc字符串转换处理:(绝对精华,收集所有的例子)

    vc字符串转换处理:(绝对精华,收集所有的例子) 1.头文件中要定义宏;   #define   UNICODE         #define   _UNICODE     //////////// ...

  8. mysql17---增量备份

    mysql增量备份: 全备份是: (增量备份一定要看日志的时间和位置节点) mysql数据库会以二进制的形式,把用户对mysql数据库的操作记录到文件中,不用使用定时器了.当用户希望恢复的时候,可以使 ...

  9. 连通图(Tarjan算法) 专题总结

    一.题目类型: 1.有向图的强连通分量: POJ1236 Network of Schools HDU1269 迷宫城堡 2.割点 & 割边: UESTC - 900 方老师炸弹 UVA315 ...

  10. ELF和a.out文件格式的比较

    本文讨论了 UNIX/LINUX 平台下三种主要的可执行文件格式:a.out(assembler and link editor output 汇编器和链接编辑器的输出).COFF(Common Ob ...