POJ 1511 Invitation Cards(逆向思维 SPFA)
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 题目大意:有p个城市,q条单向边,现在求点1到其他所有点在返回到1的距离之和的最小值。
思路:第一遍正向SPFA dis数组代表点1到其他所有点的最短距离,之后将每一条边反转,再求一遍SPFA,则dis数组代表除点1之外所有点返回到点1的最短距离
注意:由于输入数据量比较大用cin,cout妥妥的TLE,(scanf printf 也能过)。我这里就顺手打了个简单的读入
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<queue> using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int maxn = 1e6 + ;
struct node {
LL to, cost;
node() {}
node(LL a, LL b) :to(a), cost(b) {}
};
struct Task {
LL from, to, cost;
}rs[maxn];
vector<node>e[maxn];
LL n, m, T, dis[maxn], vis[maxn];
LL read() {
LL x = ;
char c = getchar();
while (c < '' || c > '')c = getchar();
while (c >= '' && c <= '') {
x = x * + c - '';
c = getchar();
}
return x;
}
void SPFA(int s)
{
for (int i = ; i <= n; i++) {
dis[i] = INF; vis[i] = ;
}
queue<int>Q;
vis[s] = ; dis[s] = ;
Q.push(s);
while (!Q.empty()) {
int t = Q.front(); Q.pop();
vis[t] = ;
for (int i = ; i < e[t].size(); i++) {
int tmp = e[t][i].to;
if (dis[tmp] > dis[t] + e[t][i].cost) {
dis[tmp] = dis[t] + e[t][i].cost;
if (!vis[tmp]) {
vis[tmp] = ;
Q.push(tmp);
}
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
T = read();
while (T--) {
n = read(), m = read();
for (int i = ; i <= n; i++)e[i].clear();//一定不能忘的初始化!
for (int i = ; i <= m; i++) {
rs[i].from = read(); rs[i].to = read(); rs[i].cost = read();
e[rs[i].from].push_back(node(rs[i].to, rs[i].cost));//正向加边
}
LL ans = ;
SPFA();
e[].clear();
for (int i = ; i <= n; i++) {
ans += dis[i]; e[i].clear();//再次初始化
}
for (int i = ; i <= m; i++)//逆向加边
e[rs[i].to].push_back(node(rs[i].from, rs[i].cost));
SPFA();
for (int i = ; i <= n; i++)ans += dis[i]; cout << ans << endl;
}
return ;
}
POJ 1511 Invitation Cards(逆向思维 SPFA)的更多相关文章
- (简单) POJ 1511 Invitation Cards,SPFA。
Description In the age of television, not many people attend theater performances. Antique Comedians ...
- POJ 1511 Invitation Cards 正反SPFA
题意:学生从A站到B站花费C元,将学生每天从‘1’号站送往其它所有站,再从所有站接回到‘1’号站,问着个过程最小花费是多少. 思路:因为数据很大所以要用SPFA,因为不仅要从1点连接各个点还要从各个点 ...
- 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 1511 Invitation Cards (spfa的邻接表)
Invitation Cards Time Limit : 16000/8000ms (Java/Other) Memory Limit : 524288/262144K (Java/Other) ...
- POJ 1511 Invitation Cards (最短路spfa)
Invitation Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description In the age ...
- Poj 1511 Invitation Cards(spfa)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...
- POJ 1511 Invitation Cards 链式前向星+spfa+反向建边
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 27200 Accepted: 902 ...
- SPFA算法(2) POJ 1511 Invitation Cards
原题: Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 31230 Accepted: ...
- [POJ] 1511 Invitation Cards
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 18198 Accepted: 596 ...
随机推荐
- 纯CSS3个性化圆形按钮登录表单
在线演示 本地下载
- jQuery打飞机游戏
在线演示 本地下载
- jquery鼠标悬停突出显示
在线演示 本地下载
- python 常见包中的不定参数
- Effective C++: 05实现
26:尽可能延后变量定义式的出现时间 1:只要你定义了一个变量而其类型带有一个构造函数或析构函数,那么当程序的控制流到达这个变量定义式时,你便得承受构造成本:当这个变量离开其作用域时,你便得承受析构成 ...
- F4NNIU 的常用 Linux 命令(2019-08-24)
目录 F4NNIU 的常用 Linux 命令 停止防火墙 查看 IP 址 启动 deepin 的桌面 查看当前时区 查看 CPU 和内存信息 用户相关 日志 F4NNIU 的常用 Linux 命令 记 ...
- Java练习 SDUT-1580_闰年
闰年 Time Limit: 1000 ms Memory Limit: 32768 KiB Problem Description 时间过得真快啊,又要过年了,同时,我们的人生也增长了一年的阅历,又 ...
- 云原生生态周报 Vol. 8 | Gartner 发布云原生趋势
业界要闻 Gartner 发布云原生基础设施未来的八大趋势:权威分析机构 Gartner 在对 2020 年技术趋势的展望当中指出:“预计2020年所有领先的容器管理软件均内置服务融合技术,到2022 ...
- 14-1 jquery的dom操作和事件对象
一 jquery的操作有,插入,修改,删除,克隆.具体见下方代码实例: <!DOCTYPE html> <html lang="en"> <head& ...
- Python基础:26模块
一:模块和文件 1:模块是逻辑上组织 Python 代码的方法,文件是物理层上组织模块的方法.因此,一个文件被看作是一个独立模块,一个模块也可以被看作是一个文件. 模块的文件名就是模块的名字加上扩展名 ...