HDU 1535 SPFA 前向星存图优化
Invitation Cards
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2972 Accepted Submission(s): 1385
The transport system is very special: all lines are unidirectional and connect exactly two stops. Buses leave the originating stop with passangers each half an hour. After reaching the destination stop they return empty to the originating stop, where they wait until the next full half an hour, e.g. X:00 or X:30, where 'X' denotes the hour. The fee for transport between two stops is given by special tables and is payable on the spot. The lines are planned in such a way, that each round trip (i.e. a journey starting and finishing at the same stop) passes through a Central Checkpoint Stop (CCS) where each passenger has to pass a thorough check including body scan.
All the ACM student members leave the CCS each morning. Each volunteer is to move to one predetermined stop to invite passengers. There are as many volunteers as stops. At the end of the day, all students travel back to CCS. You are to write a computer program that helps ACM to minimize the amount of money to pay every day for the transport of their employees.
2 2
1 2 13
2 1 33
4 6
1 2 10
2 1 60
1 3 20
3 4 10
2 4 5
4 1 50
210
{
int pr;//同起点的上一条边
int to;//单向边的终点
int w;//边的权值
} edge1[MAXN],edge2[MAXN];// 存边
加边操作
void add1(int a,int b,int c) //a-->b
{
nedge1++;//初始为零
edge1[nedge1].to=b;
edge1[nedge1].w=c;
edge1[nedge1].pr=pre1[a];
pre1[a]=nedge1;//以a为起点的下一条边的位置(位置其实就是边的序号) pre1数组初始为0;
//(发现这里是逆向的 一层一层的往上找 指导 pre1数组的值为0 ) 遍历的时候
}
SPFA 姿势
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#define MAXN 1000001
#define inf 100000000
using namespace std;
struct node
{
int pr;
int to;
int w;
}edge1[MAXN],edge2[MAXN];
int pre1[MAXN],pre2[MAXN];
int dis1[MAXN],dis2[MAXN];
int nedge1=;
int nedge2=;
int n;
int p,q;
int be,ed,we;
queue<int >qq;
int now;
int vis[MAXN];
void add1(int a,int b,int c)
{
nedge1++;
edge1[nedge1].to=b;
edge1[nedge1].w=c;
edge1[nedge1].pr=pre1[a];
pre1[a]=nedge1;
}
void add2(int a,int b,int c)
{
nedge2++;
edge2[nedge2].to=b;
edge2[nedge2].w=c;
edge2[nedge2].pr=pre2[a];
pre2[a]=nedge2;
}
void spfa1()
{
for(int gg=;gg<=p;gg++)
{
vis[gg]=;
dis1[gg]=inf;
}
vis[]=;
dis1[]=;
qq.push();
while(!qq.empty())
{
now=qq.front();
vis[now]=;
qq.pop();
for(int k=pre1[now];k!=;k=edge1[k].pr)
{
int mm=edge1[k].to;
if(dis1[now]+edge1[k].w<dis1[mm])
{
dis1[mm]=dis1[now]+edge1[k].w;
if(!vis[mm])
{
vis[mm]=;
qq.push(mm);
}
}
}
}
}
void spfa2()
{
for(int gg=;gg<=p;gg++)
{
vis[gg]=;
dis2[gg]=inf;
}
vis[]=;
dis2[]=;
qq.push();
while(!qq.empty())
{
now=qq.front();
qq.pop();
vis[now]=;
for(int k=pre2[now];k!=;k=edge2[k].pr)
{
int mm=edge2[k].to;
if(dis2[now]+edge2[k].w<dis2[mm])
{
dis2[mm]=dis2[now]+edge2[k].w;
if(!vis[mm])
{
vis[mm]=;
qq.push(mm);
}
}
}
}
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d %d",&p,&q);
nedge1=;nedge2=;
for(int gg=;gg<=p;gg++)
{
pre1[gg]=;
pre2[gg]=;
}
for(int j=;j<=q;j++)
{
scanf("%d %d %d",&be,&ed,&we);
add1(be,ed,we);
add2(ed,be,we);
}
spfa1();
spfa2();
int sum=;
for(int e=;e<=p;e++)
sum=sum+dis1[e]+dis2[e];
printf("%d\n",sum);
}
return ;
}
HDU 1535 SPFA 前向星存图优化的更多相关文章
- 最短路 spfa 算法 && 链式前向星存图
推荐博客 https://i.cnblogs.com/EditPosts.aspx?opt=1 http://blog.csdn.net/mcdonnell_douglas/article/deta ...
- Pants On Fire(链式前向星存图、dfs)
Pants On Fire 传送门:链接 来源:upc9653 题目描述 Donald and Mike are the leaders of the free world and haven't ...
- C++算法 链式前向星存图
这个东西恶心了我一阵子,那个什么是什么的上一个一直是背下来的,上次比赛忘了,回来有个题也要用,只能再学一遍,之前也是,不会为什么不学呢.我觉得是因为他们讲的不太容易理解,所以我自己给那些不会的人们讲一 ...
- UESTC 30.最短路-最短路(Floyd or Spfa(链式前向星存图))
最短路 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 在每年的校赛里,所有进入决赛的同 ...
- POJ 1985.Cow Marathon-树的直径-树的直径模板(BFS、DFS(vector存图)、DFS(前向星存图))
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 7536 Accepted: 3559 Case ...
- 链式前向星存树图和遍历它的两种方法【dfs、bfs】
目录 一.链式前向星存图 二.两种遍历方法 一.链式前向星存图:(n个点,n-1条边) 链式前向星把上面的树图存下来,输入: 9 ///代表要存进去n个点 1 2 ///下面是n-1条边,每条边连接两 ...
- UESTC30-最短路-Floyd最短路、spfa+链式前向星建图
最短路 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 在每年的校赛里,所有进入决赛的同 ...
- POJ——3159Candies(差分约束SPFA+前向星+各种优化)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 28071 Accepted: 7751 Descrip ...
- POJ 1655 Balancing Act ( 树的重心板子题,链式前向星建图)
题意: 给你一个由n个节点n-1条边构成的一棵树,你需要输出树的重心是那个节点,以及重心删除后得到的最大子树的节点个数size,如果size相同就选取编号最小的 题解: 树的重心定义:找到一个点,其所 ...
随机推荐
- HDU-1496(哈希表)
Hash入门第一题 题意: 问题描述 考虑具有以下形式的方程: a * x1 ^ 2 + b * x2 ^ 2 + c * x3 ^ 2 + d * x4 ^ 2 = 0 a,b,c,d是来自区间[- ...
- lintcode172 删除元素
删除元素 给定一个数组和一个值,在原地删除与值相同的数字,返回新数组的长度. 元素的顺序可以改变,并且对新的数组不会有影响. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一个数组 [0, ...
- GET请求的写法-jmeter
第一种写法:可以向post 请求一样写 第二种写法: /pinter/com/getSku?id=${__Random(1,100,rdmNum)}
- 完全背包问题 :背包dp
题目描述: 有 N种物品和一个容量是 V 的背包,每种物品都有无限件可用.第 i 种物品的体积是 vi,价值是 wi. 求解将哪些物品装入背包,可使这些物品的总体积不超过背包容量,且总价值最大.输出最 ...
- Java经典问题
1.JAVA初学者都应该搞懂的问题 对于这个系列里的问题,每个学Java的人都应该搞懂.当然,如果只是学Java玩玩就无所谓了.如果你认为自己已经超越初学者了,却不很懂这些问题,请将你自己重归初学者行 ...
- POJ 1873 The Fortified Forest(枚举+凸包)
Description Once upon a time, in a faraway land, there lived a king. This king owned a small collect ...
- JS中通过数组的方式操作字符串 数组是个好东西 ....
题目:使用JS将 var str="what are you nong sha lei",通过您的方法转换为"What Are You Nong Sha Lei" ...
- 自测之Lesson3:makefile
题目:编写一个makefile文件,要求编译当前目录内的所有.c文件. 完成代码: .PHONY:clean all SRC=$(wildcard *.c) BIN=$(SRC:%.c=%) all: ...
- TCP系列38—拥塞控制—1、概述
在本篇中我们继续上一篇文章wireshark的示例讲解,上一篇介绍了一个综合示例后,本篇介绍一些简单的示例,在读本篇前建议先把上一篇读完,为了节省篇幅,本篇只针对一些特殊的场景点报文进行讲解,不会像上 ...
- python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory
#vi /etc/ld.so.conf.d/python2.7.conf 加入/usr/local/python27/lib 保存退出后执行 #ldconfig