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相同就选取编号最小的 题解: 树的重心定义:找到一个点,其所 ...
随机推荐
- Java开发工程师(Web方向) - 03.数据库开发 - 期末考试
期末考试 编程题 本编程题包含4个小题,覆盖知识点从基础的JDBC.连接池到MyBatis. 1(10分) 有一款在线教育产品“天天向上”主要实现了在手机上查看课程表的功能.该产品的后端系统有一张保存 ...
- angular-使用定时器调后台接口
今天写了一个功能,一个是在两个页面中每隔一秒就调用一个后台接口 首先,这个功能使用了JS里的定时器.JS计时器分为一次性计时器和间隔性触发计时器,此次每隔一秒要调用这个接口,使用的是间隔性触发计时器 ...
- 【WXS数据类型】Array
属性: 名称 值类型 说明 [Array].constructor [String] 返回值为“Array”,表示类型的结构字符串 [Array].length [Number] 返回数组长度 方法: ...
- CSS让内部元素以相反的顺序显示
代码如下: <div id="main" style=" flex-direction: row-reverse;-webkit-flex-direction: r ...
- 使用maven构建web项目(简易版)
在eclipse中使用maven开发一个web项目 第一步:安装maven:在Windows上安装Maven 中间省略很多步骤....(包括关于eclipse中配置maven) 第二步:不用懂任何ma ...
- 地牢逃脱(BFS(广度优先搜索))
题目描述 给定一个 n 行 m 列的地牢,其中 '.' 表示可以通行的位置,'X' 表示不可通行的障碍,牛牛从 (x0 , y0 ) 位置出发,遍历这个地牢,和一般的游戏所不同的是,他每一步只能按照一 ...
- 【C#】arcface人脸识别使用问题分析
arcface上线了新版 正好有空 赶紧下载体验了一番 凡是过程中也遇到一些问题 1.初始化 [DllImport("libarcsoft_face_engine.dll", En ...
- 论文笔记:Deep Attentive Tracking via Reciprocative Learning
Deep Attentive Tracking via Reciprocative Learning NIPS18_tracking Type:Tracking-By-Detection 本篇论文地主 ...
- openstack对接VMware浅析
前言 本文是对openstack对接vmware的浅析,所以本文重点是以下两点: 先了解它的整体架构,搞清楚为什么要用这样的架构: 然后再了解架构中的各个组件,组件提供的主要功能与各个组件之间的交互 ...
- centos+nginx+redmine+gitosis安装指南
说明 这篇文章我现在的主要目的是记录自己安装redmine和gitosis的过程,可能写的有些糙,请各位读者见谅.我会在后面的时间里逐渐完善细节.但我想,这已经是网上迄今为止国内最详细的nginx+r ...