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 ...
 
随机推荐
- windows安装ZIP压缩版的Weblogic Server
			
以前要装Weblogic Server的时候都是装的安装版,最近发现ZIP版本的Weblogic Server是一个只包含Weblogic Server的版本,于是就想着弄一下它. 这里用到的Webl ...
 - Oracle 10g 升级至10.2.0.4
			
http://weihaoluo.blog.163.com/blog/static/224418832010112102355652/ 单实例环境 Vmware Server 1.0.6 +Cen ...
 - Linux安全应用之防垃圾邮件server的构建
			
Linux安全应用之防垃圾邮件server的构建 一.垃圾邮件产生的原因 垃圾邮件(SPAM) 也称作UCE(Unsoticited Commercial Email.未经许可的商业电子邮件)或UBE ...
 - Redis5.0之Stream案例应用解读
			
非常高兴有机会和大家在这里交流Redis5.0之Stream应用.今天的分享更多的是一个抛砖引玉,欢迎大家提出更多关于Redis的思考. 首先,我们来个假设,这里有个杯子,这个杯子是去年我老婆送的,送 ...
 - How to get service execuable path
			
Some time we need to get specific service path and then do something you want. there are 2 way to ge ...
 - 目前使用过的各大厂商rtsp取流的url
			
目前使用过的各大厂商取流规则是在实际的工作中遇到的相关视频接入问题,通过rtsp协议接入视频数据的一些记录,其中的图片可能来源于网络,内容部分来源于网络,本人仅仅是对相关内容作了汇总. 海康RTSP取 ...
 - Orange's_1_win7下搭建环境
			
工欲善其事,必先利其器. 由于公司电脑工作环境是win7,为了学习于渊的Orange,所以就在windows下配置环境: 1.nasm: nasm汇编 http://www.nasm.us/ ...
 - enterText与typeText
			
转自:http://www.cnblogs.com/hyddd/p/4126979.html 问题场景: Robotium.enterText输入数据后,点击"发送"按钮,多数情况 ...
 - 64位windows上访问64位oracle 12c
			
64位windows上访问64位oracle 12c,这会有啥问题? 没啥问题.问题是,我64位操作系统的机器上装了个oracle 10g.而oracle 10g好像是不区分啥32位.64位的,一律3 ...
 - iOS开发——高级篇——FMDB 数据库简单使用
			
#import <Foundation/Foundation.h> @interface UserDB : NSObject // 把userDB设计成一个单例类 + (id)shareI ...