POJ1511 Invitation Cards —— 最短路spfa
题目链接:http://poj.org/problem?id=1511
Time Limit: 8000MS | Memory Limit: 262144K | |
Total Submissions: 29286 | Accepted: 9788 |
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
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e6+; int n, m; struct edge
{
int from, to, w, next;
}edge[MAXN*];
int cnt, head[MAXN]; void add(int u, int v, int w)
{
edge[cnt].from = u;
edge[cnt].to = v;
edge[cnt].w = w;
edge[cnt].next = head[u];
head[u] = cnt++;
} void init()
{
cnt = ;
memset(head, -, sizeof(head));
} LL dis1[MAXN], dis2[MAXN];
int times[MAXN], inq[MAXN];
void spfa(int st, LL dis[])
{
memset(inq, , sizeof(inq));
memset(times, , sizeof(times));
for(int i = ; i<=n; i++)
dis[i] = LNF; queue<int>Q;
Q.push(st);
inq[st] = ;
dis[st] = ;
while(!Q.empty())
{
int u = Q.front();
Q.pop(); inq[u] = ;
for(int i = head[u]; i!=-; i = edge[i].next)
{
int v = edge[i].to;
if(dis[v]>1LL*dis[u]+1LL*edge[i].w)
{
dis[v] = 1LL*dis[u]+1LL*edge[i].w;
if(!inq[v])
{
Q.push(v);
inq[v] = ;
}
}
}
}
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
scanf("%d%d", &n, &m);
for(int i = ; i<m; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
add(u,v,w);
} spfa(, dis1);
init();
/**一开始想直接取反,即swap(edge[i].from, edge[i].to),
后来发现处理不了head[]数组, 所以还是重新建边 */
for(int i = ; i<m; i++) //m为原来的边数, 即cnt
add(edge[i].to, edge[i].from, edge[i].w);
spfa(, dis2); LL ans = ;
for(int i = ; i<=n; i++)
ans += dis1[i]+dis2[i]; printf("%lld\n", ans);
}
}
POJ1511 Invitation Cards —— 最短路spfa的更多相关文章
- POJ-1511 Invitation Cards( 最短路,spfa )
题目链接:http://poj.org/problem?id=1511 Description In the age of television, not many people attend the ...
- poj1511/zoj2008 Invitation Cards(最短路模板题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Invitation Cards Time Limit: 5 Seconds ...
- POJ-1511 Invitation Cards (双向单源最短路)
Description In the age of television, not many people attend theater performances. Antique Comedians ...
- POJ1511:Invitation Cards(最短路)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 34743 Accepted: 114 ...
- HDU 1535 Invitation Cards (最短路)
题目链接 Problem Description In the age of television, not many people attend theater performances. Anti ...
- J - Invitation Cards 最短路
In the age of television, not many people attend theater performances. Antique Comedians of Malidine ...
- POJ1511 Invitation Cards(多源单汇最短路)
边取反,从汇点跑单源最短路即可. #include<cstdio> #include<cstring> #include<queue> #include<al ...
- POJ-1511 Invitation Cards 往返最短路 邻接表 大量数据下的处理方法
题目链接:https://cn.vjudge.net/problem/POJ-1511 题意 给出一个图 求从节点1到任意节点的往返路程和 思路 没有考虑稀疏图,上手给了一个Dijsktra(按紫书上 ...
- POJ-1511 Invitation Cards (单源最短路+逆向)
<题目链接> 题目大意: 有向图,求从起点1到每个点的最短路然后再回到起点1的最短路之和. 解题分析: 在求每个点到1点的最短路径时,如果仅仅只是遍历每个点,对它们每一个都进行一次最短路算 ...
随机推荐
- 还是Tomcat,关于类加载器的趣味实验
一.前言 类加载器,其实是很复杂一个东西,想等到我完全什么都弄明白了再写出来,估计不太现实...现在只能是知道多少写多少吧. 首先,我提一个问题:在我们自己的servlet中(比如ssm中,contr ...
- TYVJ4623 球球大作战·生存
时间: 500ms / 空间: 65536KiB / Java类名: Main 背景 小天很喜欢玩球球大作战这个游戏,大家也应该都玩过.游戏规则是:移动自己的球,移动到别人的球(一定要比自己的球小)的 ...
- Codeforces Round #275 (Div. 2) B. Friends and Presents 二分+数学
8493833 2014-10-31 08:41:26 njczy2010 B - Friends and Presents G ...
- Laravel 报500错误
Laravel报500错误 发生情境: 使用Composer安装Laravel5.1版本到本地wamp环境,可以成功访问框架首页,然后上传到服务器上,报500错误. 解决: (1)在首页public/ ...
- Servlet 2.4 规范之第二篇:Servlet接口
Servlet接口是Servlet API的最核心抽象类.所有的servlets都直接实现了这个接口,或者以更通用的方式继承了这个接口的实现类.Servlet API自带了两个实现了Servlet接口 ...
- (入门SpringBoot)SpringBoot项目数据源以及整合mybatis(二)
1.配置tomcat数据源: # 数据源基本配置spring.datasource.url=jdbc:mysql://localhost:3306/shoptest?useUnicode=true ...
- File类 文件过滤器
创建过滤器 package cn.zmh.File; import java.io.File; import java.io.FileFilter; // 实现FileFilter类的功能 publi ...
- Java的不定参数(eg:Object...)(转)
第一个例子: public class VariArgs { public static void main(String[] args) { test(); test("aaa" ...
- 识别SQL Server 性能杀手
性能优化的重点在于识别定位问题,预先了解主要的性能杀手,能够更快的定位到问题并将工作集中在可能的原因之上. SQL SERVER性能杀手主要集中在如下几类: 1.1 低质量的索引 低质量的索引通常 ...
- 【spring boot Mybatis】报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.newhope.interview.dao.UserMapper.add
报错如下: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.newhope.i ...