poj 1511 spfa 模板题
来源:互联网 发布:端口聚合命令 编辑:程序博客网 时间:2024/09/21 06:37
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
22 21 2 132 1 334 61 2 102 1 601 3 203 4 102 4 54 1 50
Sample Output
46210
Source
edge[i].to表示第i条边的终点,edge[i].next表示与第i条边同起点的下一条边的存储位置,edge[i].w为边权值.
另外还有一个数组head[],它是用来表示以i为起点的第一条边存储的位置,实际上你会发现这里的第一条边存储的位置其实
在以i为起点的所有边的最后输入的那个编号.
head[]数组一般初始化为-1,对于加边的add函数是这样的:
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
using namespace std;
const int MAXN = 1000005;
const int inf = 0x3f3f3f3f;
typedef long long LL;
int head[2][MAXN];
bool vis[MAXN];
LL dist[MAXN];
struct node{
int to, next, w;
} edge[2][MAXN];
int n, m;
void spfa(int cur)
{
int v, i, b;//v为以v编号为起点
queue <int> q;
for(int i = 1; i<=n; ++i)
{
vis[i] = false;
dist[i] = inf;
}
while(!q.empty())
q.pop();
q.push(1);
vis[1] = 1;
dist[1] = 0;
while(!q.empty())
{
v = q.front();
q.pop();
vis[v] = 0;
for(i = head[cur][v]; ~i; i=edge[cur][i].next)//head[cur][v]就是v为起点的对应第几条边,若不为-1则存在到其他点的路径
{
//这里的i是以v为起点,边的编号
b = edge[cur][i].to;//从编号最大的终点开始,初始化时的i和head【a】=i中的i相等。
if(dist[b]>dist[v] + edge[cur][i].w)//以起点为v初始化时的,终点为i
{
dist[b] = dist[v] + edge[cur][i].w;
if(!vis[b])
{
vis[b] = true;
q.push(b);
}
}
}
}
}
int main()
{
int t;
int a, b, w;
scanf("%d", &t);
while(t--)
{
scanf("%d %d", &n, &m);
for(int i = 1; i<=n; ++i)
{
head[0][i] = -1;
head[1][i] = -1;
}
for(int i = 0; i<m; ++i)
{
scanf("%d %d %d", &a, &b, &w);
edge[0][i].w = w;
edge[0][i].to = b;
edge[0][i].next = head[0][a];
head[0][a] = i;
edge[1][i].w = w;
edge[1][i].to = a;
edge[1][i].next = head[1][b];
head[1][b] = i;
}
LL ans = 0;
spfa(0);
for(int i = 1; i<=n; ++i) ans += dist[i];
spfa(1);
for(int i = 1; i<=n; ++i) ans += dist[i];
printf("%lld\n", ans);
}
return 0;
}
- poj 1511 spfa 模板题
- POJ 1511 SPFA 模板
- POJ-1511 初探Bellman-Ford,再水SPFA模板题..
- poj 1511 Invitation Cards 最短路SPFA(模板题)
- POJ 3259-Wormholes (Bellman-Ford&&SPFA) (模板题)
- POJ 2447 Remmarguts' Date【k短路 SPFA+A* 模板题】
- poj 3259 Wormholes(spfa) (spfa模板)
- poj-1125-Stockbroker Grapevine spfa模板
- POJ 3169(差分约束 + spfa模板)
- poj 1511 spfa
- poj 1511 spfa
- poj 1511----spfa
- POJ 1511 spfa
- poj 1511 SPFA
- poj 1511 spfa入门
- poj 1511 SPFA
- poj 1511 spfa水题
- HDU 1535 && POJ 1511 Invitation Cards (SPFA 模板 + 反向建图)
- 机器学习之先验分布,后验分布,共轭先验分布
- SSH框架搭建
- strip 用法
- Java 流(Stream)、文件(File)和IO
- react-webpack 照片墙制作(二)
- poj 1511 spfa 模板题
- WUSTOJ 1961 神奇的序列
- 洛谷树
- C中printf与scanf的使用方法
- strip函数
- 尚观嵌入式c语言编程进阶笔记——2.数组
- 【名词】JPA、JTA、JMS、CRUD
- 任务,线程和同步(五)之(Thread类)线程类
- UVA 1586 分子量(简单模拟计算)