poj 1511 Invitation Cards spfa 邻接矩阵
题目链接:
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 邻接矩阵的更多相关文章
- POJ 1511 Invitation Cards (spfa的邻接表)
Invitation Cards Time Limit : 16000/8000ms (Java/Other) Memory Limit : 524288/262144K (Java/Other) ...
- Poj 1511 Invitation Cards(spfa)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...
- 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 / ...
- SPFA算法(2) POJ 1511 Invitation Cards
原题: Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 31230 Accepted: ...
- POJ 1511 Invitation Cards (最短路spfa)
Invitation Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description In the age ...
- (简单) POJ 1511 Invitation Cards,SPFA。
Description In the age of television, not many people attend theater performances. Antique Comedians ...
- POJ 1511 Invitation Cards 链式前向星+spfa+反向建边
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 27200 Accepted: 902 ...
- POJ 1511 Invitation Cards(逆向思维 SPFA)
Description In the age of television, not many people attend theater performances. Antique Comedians ...
- [POJ] 1511 Invitation Cards
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 18198 Accepted: 596 ...
随机推荐
- java 返回json数据
Student st1 = new Student(1, "dg", 18, new Date()); Student st2 = new Student(2 ...
- Broadcom的消息机制
在Broadcom中提供了自己的消息机制,有两种消息形式:Request/Response and Event(事件) Request/Response消息:进程之间的通信都是通过smd,所有的消息都 ...
- C# Json反序列化 数据协定类型 无法反序列化 由于未找到必需的数据成员
背景今天在使用:C# Json 序列化与反序列化 反序列化的时候出现了以下的错误信息. System.Runtime.Serialization.SerializationException: 数据协 ...
- PHP根据两点间的经纬度计算距离
/** * 说明: 根据两点间的经纬度计算距离 * @param float $lat 纬度值 * @param float $lng 经度值 */ function getDistance($lat ...
- 使用python在极坐标中生成一条直线
在测试雷达时,往往需要测试雷达的数据是否准确,这时就需要在雷达图中显示一条标准的直线作为对比. "create a wall" import numpy as np import ...
- Linux 文本编辑
文本编辑: 查看文本内容: cat:将文件连接并显示 -n:显示时将文件每一行编号 tac:类似于cat,但其功能是逆序显示每一行文件 linlin@ubuntu ...
- ==和equals的差别
== 和 Equals 的差别 1. == 是一个运算符. 2.Equals则是string对象的方法.能够.(点)出来. 我们比較无非就是这两种 1.基本数据类型比較 2.引用对象比較 1.基本数据 ...
- Java中有多少种设计模式?请简单画一下三种常见设计模式的类图?
转载:http://blog.csdn.net/longyulu/article/details/9159589 一.设计模式的分类 总体来说设计模式分为三大类: 创建型模式,共五种:工厂方法模式.抽 ...
- mac系统下命令编译android ndk项目
1.设置ndk环境变量 2.构造android ndk项目,命令:ndk-build 3.使用ant构造android sdk项目:命令:android update project -p . -n ...
- 小程序-demo:小程序示例
ylbtech-小程序-demo:小程序示例 1.返回顶部 0. 1.app.js const openIdUrl = require('./config').openIdUrl App({ ...