Invitation Cards(邻接表+逆向建图+SPFA)
Time Limit: 8000MS | Memory Limit: 262144K | |
Total Submissions: 17538 | Accepted: 5721 |
Description
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.
Input
Output
Sample Input
2
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
Sample Output
46
210 题意:意思很简单,给一个无向图,顶点1—n,求顶点1到其他所有顶点的来回费用最小,即求顶点1到其他顶点的最小费用以及其他所有顶点到顶点1的最小费用的和。
思路:这个题给的数据是100W,如果用邻接矩阵存可能会超内存,如果用Dij可能会超时,所以就用邻接表+SPFA,建图的时候要正逆向建图,正向建图求顶点1到其他顶点的最短路,逆向建图求其他顶点到顶点1的最短路,所以两次SPFA求以顶点1为源点的最短路径的和就是最小花费。
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; const int maxn = ;
const int INF = 0x3f3f3f3f;
typedef long long LL; struct edge
{
int to,w;
struct edge *next;
}; struct edge *map1[maxn],*map2[maxn];
int n,m;
LL ans;
LL dis[maxn];
int inque[maxn]; void SPFA(int flag)
{
queue<int>que;
while(!que.empty())
que.pop();
for(int i = ; i <= n; i++)
{
dis[i] = INF;
inque[i] = ;
} dis[] = ;
inque[] = ;
que.push();
while(!que.empty())
{
int u = que.front();
que.pop();
inque[u] = ; struct edge *tmp;
if(flag == )
tmp = map1[u];
else tmp = map2[u];
while(tmp)
{
int v = tmp->to;
int w = tmp->w;
if(dis[v] > dis[u]+w)
{
dis[v] = dis[u] + w;
if(!inque[v])
{
inque[v] = ;
que.push(v);
}
}
tmp = tmp->next;
}
}
for(int i = ; i <= n; i++)
ans += dis[i];
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int u,v,w;
struct edge *tmp1,*tmp2;
memset(map1,,sizeof(map1));
memset(map2,,sizeof(map2));
scanf("%d %d",&n,&m);
for(int i = ; i < m; i++)
{
scanf("%d %d %d",&u,&v,&w);
tmp1 = new edge;//为tmp1开辟空间;正向建图
tmp1->to = v;
tmp1->w = w;
tmp1->next = NULL;
if(map1[u] == NULL)
map1[u] = tmp1;
else
{
tmp1->next = map1[u];
map1[u] = tmp1;
} tmp2 = new edge;//为tmp2开辟空间;逆向建图
tmp2->to = u;
tmp2->w = w;
tmp2->next = NULL; if(map2[v] == NULL)
map2[v] = tmp2;
else
{
tmp2->next = map2[v];
map2[v] = tmp2;
}
}
/*for(int i = 1; i <= n; i++)//输出邻接表。
{
printf("%d: ",i);
tmp1 = map1[i];
while(tmp1)
{
printf("%d ",tmp1->to);
tmp1 = tmp1->next;
}
printf("\n");
}*/
ans = ;
SPFA();//正向寻找1顶点到其他所有顶点的最短距离
SPFA();//逆向寻找1顶点到其他所有顶点的最短距离
printf("%lld\n",ans);
}
return ;
}
Invitation Cards(邻接表+逆向建图+SPFA)的更多相关文章
- POJ 1511 - Invitation Cards 邻接表 Dijkstra堆优化
昨天的题太水了,堆优化跑的不爽,今天换了一个题,1000000个点,1000000条边= = 试一试邻接表 写的过程中遇到了一些问题,由于习惯于把数据结构封装在 struct 里,结果 int [10 ...
- POJ 3687 Labeling Balls 逆向建图,拓扑排序
题目链接: http://poj.org/problem?id=3687 要逆向建图,输入的时候要判重边,找入度为0的点的时候要从大到小循环,尽量让编号大的先入栈,输出的时候注意按编号的顺序输出重量, ...
- 无向图的 DFS 和 BFS实现 (以邻接表存储的图)
#include <iostream> #include <queue> using namespace std; #define MaxVertexNum 10 typede ...
- hdu 4857 逃生 拓扑排序+逆向建图
逃生 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Descr ...
- POJ1122_FDNY to the Rescue!(逆向建图+最短路树)
FDNY to the Rescue! Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2368 Accepted: 72 ...
- POJ 1511 Invitation Cards 链式前向星+spfa+反向建边
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 27200 Accepted: 902 ...
- 数据结构 《2》----基于邻接表表示的图的实现 DFS(递归和非递归), BFS
图通常有两种表示方法: 邻接矩阵 和 邻接表 对于稀疏的图,邻接表表示能够极大地节省空间. 以下是图的数据结构的主要部分: struct Vertex{ ElementType element; // ...
- 图的基本操作(基于邻接表):图的构造,深搜(DFS),广搜(BFS)
#include <iostream> #include <string> #include <queue> using namespace std; //表结点 ...
- NOIP2013 华容道 (棋盘建图+spfa最短路)
#include <cstdio> #include <algorithm> #include <cstring> #include <queue> # ...
随机推荐
- HTML+CSS基础学习笔记(1)
一.了解HTML.CSS.JS 1.HTML是网页内容的载体. 内容就是网页制作者放在页面上想要让用户浏览的信息,可以包含文字.图片.视频等. 2.CSS样式是表现. 用来改变内容外观的东西称之为表现 ...
- Android 设计随便说说之简单实践(模块划分)
上篇随笔随(Android 设计随便说说)便说了一下什么是设计以及设计的原则,这里举一个简单的例子来进一步的说Android设计.我们以应用商店的设计来举例. 在设计之前,需要把握两部分内容,才能使得 ...
- sqlserver2005唯一性约束
[转载]http://blog.163.com/rihui_7/blog/static/21228514320136193392749/ 1.设置字段为主键就是一种唯一性约束的方法,如 int p ...
- SVN库迁移过程总结
一.背景:老SVN是安装在32位服务器上:现在64位服务器上安装了新版本SVN服务,所以需要将SVN从老服务器上迁移到新服务器上. 1.SVN Server下载:https://www.visuals ...
- linux du 显示目录下的各个子目录的大小
use command du display estimate file space usage size of subdirectories [oracle@ahjcyl-db backup]$ ...
- SQL SERVER while循环
在SQL数据库中,可以通过WHILE实现循环,下面就将为您介绍SQL循环执行while控制,希望对您提升WHILE的使用水平能够有些帮助. WHILE Boolean_expression { ...
- 在github上搭建博客(使用Jekyll)
简单说,只需要三步,就可以在 Github 搭建起一个博客: 在 Github 上建一个名为 xxx.github.io 的库: 把看中了的 Jekyll 模板 clone 到本地: 把这个模板 pu ...
- CSV文件规则
CSV文件规则 1 开头是不留空,以行为单位.2 可含或不含列名,含列名则居文件第一行.3 一行数据不跨行,无空行.4 以半角逗号(即,)作分隔符,列为空也要表达其存在.5 列内容如存在半角逗号(即, ...
- Codevs 1082 线段树练习 3
1082 线段树练习 3 时间限制: 3 s 空间限制: 128000 KB 题目等级 : 大师 Maste 传送门 题目描述 Description 给你N个数,有两种操作: 1:给区间[a,b]的 ...
- LATEX学习笔记1
LATEX源文件的结构分三大部分,依次为:文档类声明.序言(可选).正文. 文档结构 \documentclass{article} \usepackage{amsmath} \usepackage{ ...