(最短路 SPFA)Invitation Cards -- poj -- 1511
链接:
http://poj.org/problem?id=1511
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82829#problem/J
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
using namespace std; #define N 1000005
#define oo 0x3fffffff struct node
{
int a, b, next;
long long p;
}s1[N], s[N]; int n, m, head[N];
long long dis[N];
bool vis[N]; void Add(int a, int b, long long p, int k)
{
s1[k].a=a;
s1[k].b=b;
s1[k].p=p;
s1[k].next=head[a];
head[a]=k;
} long long spfa()
{
queue<int>Q;
Q.push();
vis[]=true; for(int i=; i<=n; i++)
dis[i]=oo;
dis[]=; while(Q.size())
{
int i = Q.front(); Q.pop();
vis[i] = false; for(int j=head[i]; j != ; j = s1[j].next)
{
int a = s1[j].a, b = s1[j].b;
int p = s1[j].p; if(dis[a]+p < dis[b])
{
dis[b] = dis[a] + p; if(vis[b] == false)
{
Q.push(b);
vis[b] = true;
}
}
}
} long long sum=; for(int i=; i<=n; i++)
sum += dis[i];
return sum;
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &n, &m); int i; memset(head, , sizeof(head));
memset(s, , sizeof(s));
memset(s1, , sizeof(s1)); for(i=; i<=m; i++)
{
scanf("%d%d%I64d", &s[i].a, &s[i].b, &s[i].p);
Add(s[i].a, s[i].b, s[i].p, i);
} long long ans = spfa(); memset(head, , sizeof(head));
for(i=; i<=m; i++)
Add(s[i].b, s[i].a, s[i].p, i); ans += spfa();
printf("%I64d\n", ans);
}
return ;
}
(最短路 SPFA)Invitation Cards -- poj -- 1511的更多相关文章
- Invitation Cards POJ - 1511 (双向单源最短路)
In the age of television, not many people attend theater performances. Antique Comedians of Malidine ...
- Invitation Cards POJ 1511 SPFA || dij + heap
http://poj.org/problem?id=1511 求解从1去其他顶点的最短距离之和. 加上其他顶点到1的最短距离之和. 边是单向的. 第一种很容易,直接一个最短路, 然后第二个,需要把边反 ...
- Invitation Cards POJ - 1511
题目链接:https://vjudge.net/problem/POJ-1511 思路:题目意思就是,从1出发到所有城市,再从所有城市回到1的最短时间. 那么我们只要正跑一次图,然后反向存边,再跑一次 ...
- SPFA算法(2) POJ 1511 Invitation Cards
原题: Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 31230 Accepted: ...
- (最短路 spfa)Wormholes -- poj -- 3259
http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions ...
- 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的邻接表)
Invitation Cards Time Limit : 16000/8000ms (Java/Other) Memory Limit : 524288/262144K (Java/Other) ...
- poj 1511 Invitation Cards(最短路中等题)
In the age of television, not many people attend theater performances. Antique Comedians of Malidine ...
- POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 16178 Accepted: 526 ...
随机推荐
- Mysql 创建外键 1005 err 150
要看错误的详细提示,可以使用命令:(在MySQL Manual里搜索“errno 150”时找到) SHOW ENGINE INNODB STATUS; //针对用INNODB存储方式的数据库 ...
- 面试总结之数据结构(Data Structure)
常用数据结构及复杂度 http://www.cnblogs.com/gaochundong/p/3813252.html 常用数据结构的时间复杂度 Data Structure Add Find De ...
- MySQL转Oracle,MyBatis Mapper XML 文件修改项总结
1.对于批量插入 需要更改成 <insert id="saveAll"> insert into(a,b,c) <foreach collection=" ...
- C# 中带有中国农历的日期选择控件
开源一款自己刚开始接触 C# 时开发的带有农历信息的日期选择控件,记得那时还是在2010年的寒假期间做的这个东西.刚开始接触 C# 时,使用WinForm来开发桌面程序,觉得简直是简单又迅速,由于 C ...
- leetcode219
public class Solution { Dictionary<int, List<int>> dic = new Dictionary<int, List< ...
- windows到ubuntu
按照xmarks同步浏览器书签. mvn, copy setting.xml 最好不要用apt-get install maven, 占用/的磁盘空间 mvn -U package -P"d ...
- Linux CentOS修改网卡IP/网关设置
1. 修改对应网卡IP的配置文件 修改以下内容 2. 修改对应网卡的网关的配置文件 vi /etc/sysconfig/network 修改以下内容 3. CentOS 修改DNS 修改以下内容 4. ...
- python's output redirect
[python's output redirect] fin = open("xx.txt", 'r'); print >>fin, "hello world ...
- SIEBEL GET最新时提示表异常
无法GET最新,则将此表GET一次,CHECK OUT下来,再UNDO CHECK IN即可
- mysql查询大于X分钟数
select * from table where date_add(STR_TO_DATE(createtime,'%Y-%m-%d %T:%i:%s'), interval '00:60:00 ...