POJ 1511 Invitation Cards 链式前向星+spfa+反向建边
Time Limit: 8000MS | Memory Limit: 262144K | |
Total Submissions: 27200 | Accepted: 9022 |
Description
with all the necessary information and with the programme. A lot of students were hired to distribute these invitations among the people. Each student volunteer has assigned exactly one bus stop and he or she stays there the whole day and gives invitation
to people travelling by bus. A special course was taken where students learned how to influence people and what is the difference between influencing and robbery.
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
including CCS and Q the number of bus lines. Then there are Q lines, each describing one bus line. Each of the lines contains exactly three numbers - the originating stop, the destination stop and the price. The CCS is designated by number 1. Prices are positive
integers the sum of which is smaller than 1000000000. You can also assume it is always possible to get from any stop to any other stop.
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出发然后再重新回到1最关路径的总和。
线正向跑一边spfa。然后将输入的边反向建图在跑一次,两次累加的和就是结果。
数据较大,最后的累加和用long long。
反向建边在输入的时候与正向建边同时完成。开成一个二维的结构体编辑数组。第0维表示正向的,第1维表示反向的。
代码:
#include<iostream>
#include<string>
#include<algorithm>
#include<queue>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std;
const int maxn=1000005;
const int maxm=2000005;
const int INF=0x3f3f3f3f;
struct edgenode {
int to,next;
long long w;
}edges[2][maxm];
int head[2][maxn],du[maxn];
long long dist[maxn];
bool vis[maxn];
int n,cnt;
bool spfa(int flag) {
memset(vis,false,sizeof(vis));
memset(du,0,sizeof(du));
memset(dist,INF,sizeof(dist));
queue<int> q;
vis[1]=true;dist[1]=0;
q.push(1);
while(!q.empty()) {
int now=q.front();q.pop();
vis[now]=false;
++du[now];
if(du[now]>n) return false;
for(int i=head[flag][now];~i;i=edges[flag][i].next) {
if(dist[edges[flag][i].to]>edges[flag][i].w+dist[now]) {
dist[edges[flag][i].to]=dist[now]+edges[flag][i].w;
if(!vis[edges[flag][i].to]) {
vis[edges[flag][i].to]=true;
q.push(edges[flag][i].to);
}
}
}
}
return true;
}
void addedge(int u, int v, long long w, int flag) {
edges[flag][cnt].to=v;
edges[flag][cnt].w=w;
edges[flag][cnt].next=head[flag][u];
head[flag][u]=cnt;
}
int main() {
int t,m;
scanf("%d",&t);
while(t--) {
scanf("%d%d",&n,&m);
int u,v;long long w,sum=0;
cnt=0;
for(int i=0;i<maxm;++i) {edges[0][i].next=-1;edges[1][i].next=-1;}
for(int i=0;i<maxn;++i) {head[0][i]=-1;head[1][i]=-1;}
for(int i=0;i<m;++i) {
scanf("%d%d%lld",&u,&v,&w);
addedge(u,v,w,0);
addedge(v,u,w,1);
++cnt;
}
if(spfa(0)) for(int i=2;i<=n;++i) sum+=dist[i];
if(spfa(1)) for(int i=2;i<=n;++i) sum+=dist[i];
printf("%lld\n",sum);
}
return 0;
}
POJ 1511 Invitation Cards 链式前向星+spfa+反向建边的更多相关文章
- 链式前向星+SPFA
今天听说vector不开o2是数组时间复杂度常数的1.5倍,瞬间吓傻.然后就问好的图表达方式,然后看到了链式前向星.于是就写了一段链式前向星+SPFA的,和普通的vector+SPFA的对拍了下,速度 ...
- POJ 3169 Layout(差分约束+链式前向星+SPFA)
描述 Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 ...
- 【模板】链式前向星+spfa
洛谷传送门--分糖果 博客--链式前向星 团队中一道题,数据很大,只能用链式前向星存储,spfa求单源最短路. 可做模板. #include <cstdio> #include <q ...
- POJ 1511 链式前向星+SPFA
#include<iostream> #include<cstdio> #include<cstdlib> using namespace std; const i ...
- 洛谷 P3371 【模板】单源最短路径 【链式前向星+SPFA】
题目描述 如题,给出一个有向图,请输出从某一点出发到所有点的最短路径长度. 输入输出格式 输入格式: 第一行包含三个整数N.M.S,分别表示点的个数.有向边的个数.出发点的编号. 接下来M行每行包含三 ...
- poj 1511 Invitation Cards(最短路中等题)
In the age of television, not many people attend theater performances. Antique Comedians of Malidine ...
- 链式前向星版DIjistra POJ 2387
链式前向星 在做图论题的时候,偶然碰到了一个数据量很大的题目,用vector的邻接表直接超时,上网查了一下发现这道题数据很大,vector可定会超的,不会指针链表的我找到了链式前向星这个好东西,接下来 ...
- 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 / ...
- # [Poj 3107] Godfather 链式前向星+树的重心
[Poj 3107] Godfather 链式前向星+树的重心 题意 http://poj.org/problem?id=3107 给定一棵树,找到所有重心,升序输出,n<=50000. 链式前 ...
随机推荐
- python、java和php的百度指数对比
Python: Python是纯粹的自由软件, 源代码和解释器CPython遵循 GPL(GNU General Public License)协议.Python语法简洁清晰,特色之一是强制用空白符( ...
- Java:什么是面向对象?
1.首先我们去区分对象的属性和方法. 一).什么是对象? 比如:所有的东西都可以叫做对象.而对象就是提供给我们研究对象,这就是对象. 二).怎么区分"属性"和"方法&qu ...
- Ubuntu配置OpenStack 二:配置时间同步NTP和安装数据库Maridb以及问题总结
继上一节Ubuntu配置OpenStack 一:配置主机环境,下面继续为安装时间同步,以及配置openstack的安装包源和安装数据库Maridb.(全文截图都是由自己徒手搭建完成并且截图) 一.安装 ...
- 0_Simple__cppIntegration
引用已经编好的 .cu 和 .cpp 代码来混合使用.在 main.cpp 中调用了使用GPU的 cppIntegration.cu (测试函数也在其中) 和使用CPU的 cppIntegration ...
- JQuery源码-------JQuery中数值型变量的判断isNumeric
判断一个数值型变量的方法,在jquery中非常简单,只有一行代码. isNumeric: function( obj ) { // parseFloat NaNs numeric-cast false ...
- Magicodes.Admin.Core开源框架总体介绍
框架说明 Magicodes.Admin.Core框架在ABP以及ASP.NET ZERO的基础上进行了封装和完善,目前基于.NET Core 2.0+(Framework版本),由于部分组件在.NE ...
- Maven中settings.xml的配置项说明精讲
1.Maven的配置文件(Maven的安装目录/conf/settings.xml ) 和 Maven仓库下(默认的Maven仓库的是用户家目录下的.m2文件,可以另行制定)的settings.xml ...
- C#编程命名规范推荐
1.用Pascal规则来命名方法和类型. Pascal 大小写形式是指名称中的单词的第一个字母大写public class DataGrid{public void DataBind(){ }} 2. ...
- c# asp.net 多数组索引的解决方法
本人今天做了一个功能 需要在一个类里用多个数组, 数组需要索引器来调用 一个数组 我查了msdn 一个类里面只能有一个this 索引器 那这么多数组如何构造索引呢 我在坛子里找到了解决之道 view ...
- 怎样在一个HTML中嵌入另一个HTML页面(iframe标签用法)
iframe 怎么 在一个网页中嵌入另一个网页呢,我们可以用html中的iframe标签搞定. iframe支持所有浏览器.下面来看语法: <iframe src="规定在 ifram ...