当前位置:网站首页>P3008 [usaco11jan]roads and planes g (SPFA + SLF optimization)
P3008 [usaco11jan]roads and planes g (SPFA + SLF optimization)
2022-07-02 13:45:00 【Joanh_ Lan】
The title is as follows :
[USACO11JAN]Roads and Planes G
Topic translation
Question description
Farmer John He is investigating his milk sales plan in a new sales area . He wants to deliver the milk T T T A town ( 1 ≤ T ≤ 25 , 000 1 \le T \le 25,000 1≤T≤25,000 ), The number is 1 1 1 To T T T . Between these towns R R R road ( 1 ≤ R ≤ 50 , 000 1 \le R \le 50,000 1≤R≤50,000 , The number is 1 1 1 To R R R ) and P P P Routes ( 1 ≤ P ≤ 50 , 000 1 \le P \le 50,000 1≤P≤50,000 , The number is 1 1 1 To P P P ) Connect . Each road i i i Or route i i i Connecting towns A i A_i Ai ( 1 ≤ A i ≤ T 1 \le A_i \le T 1≤Ai≤T ) To B i B_i Bi ( 1 ≤ B i ≤ T 1 \le B_i \le T 1≤Bi≤T ), Cost is C i C_i Ci .
For roads 0 ≤ C i ≤ 10 , 000 0 \le C_i \le 10,000 0≤Ci≤10,000 ; However, the cost of the route is amazing , cost C i C_i Ci It could be a negative number ( − 10 , 000 ≤ C i ≤ 10 , 000 -10,000 \le C_i \le 10,000 −10,000≤Ci≤10,000 ). The road is two-way , It can be downloaded from A i A_i Ai To B i B_i Bi, You can also get it from B i B_i Bi To A i A_i Ai , The cost is C i C_i Ci . However, the route is different , Only from A i A_i Ai To B i B_i Bi .
in fact , Because of the recent arrogance of terrorism , For social harmony , a Some policy guarantees : If there is a route from A i A_i Ai To B i B_i Bi, So it's impossible to get from China through some roads and routes B i B_i Bi go back to A i A_i Ai . because F J FJ FJ Our cows are recognized as awesome in the world , He needs to transport cows to every town . He wants to find the town from the sending center S S S ( 1 ≤ S ≤ T 1 \le S \le T 1≤S≤T) The cheapest way to send cows to every town , Or know that this is impossible .
Input format
common R + P + 1 R+P+1 R+P+1 That's ok
The first 1 1 1 That's ok : Four integers T T T , R R R , P P P and S S S , Respectively represents the number of towns , Number of roads , Number of routes and central towns .
The first 2 2 2 To R + 1 R+1 R+1 That's ok : Three integers per row A i A_i Ai , B i B_i Bi and C i C_i Ci , Describe a road .
The first R + 2 R+2 R+2 To R + P + 1 R+P+1 R+P+1 That's ok : Three integers per row A i A_i Ai , B i B_i Bi and C i C_i Ci , Describe a route .
Output format
common T T T That's ok , The first i i i Row output City S S S To the city i i i Minimum cost of . If you can't get to , Output NO PATH
Title Description
Farmer John is conducting research for a new milk contract in a new territory. He intends to distribute milk to T (1 <= T <= 25,000) towns conveniently numbered 1…T which are connected by up to R (1 <= R <= 50,000) roads conveniently numbered 1…R and/or P (1 <= P <= 50,000) airplane flights conveniently numbered 1…P.
Either road i or plane i connects town A_i (1 <= A_i <= T) to town B_i (1 <= B_i <= T) with traversal cost C_i. For roads, 0 <= C_i <= 10,000; however, due to the strange finances of the airlines, the cost for planes can be quite negative (-10,000 <= C_i <= 10,000).
Roads are bidirectional and can be traversed from A_i to B_i or B_i to A_i for the same cost; the cost of a road must be non-negative.
Planes, however, can only be used in the direction from A_i to B_i specified in the input. In fact, if there is a plane from A_i to B_i it is guaranteed that there is no way to return from B_i to A_i with any sequence of roads and planes due to recent antiterror regulation.
Farmer John is known around the world as the source of the world’s finest dairy cows. He has in fact received orders for his cows from every single town. He therefore wants to find the cheapest price for delivery to each town from his distribution center in town S (1 <= S <= T) or to know that it is not possible if this is the case.
MEMORY LIMIT: 64MB
Input format
* Line 1: Four space separated integers: T, R, P, and S
* Lines 2…R+1: Three space separated integers describing a road: A_i, B_i and C_i
* Lines R+2…R+P+1: Three space separated integers describing a plane: A_i, B_i and C_i
Output format
* Lines 1…T: The minimum cost to get from town S to town i, or ‘NO PATH’ if this is not possible
Examples #1
The sample input #1
6 3 3 4
1 2 5
3 4 5
5 6 10
3 5 -100
4 6 -100
1 3 -10
Sample output #1
NO PATH
NO PATH
5
0
-95
-100
Tips
6 towns. There are roads between town 1 and town 2, town 3 and town 4, and town 5 and town 6 with costs 5, 5 and 10; there are planes from town 3 to town 5, from town 4 to town 6, and from town 1 to town 3 with costs -100, - 100 and -10. FJ is based in town 4.
FJ’s cows begin at town 4, and can get to town 3 on the road. They can get to towns 5 and 6 using planes from towns 3 and 4. However, there is no way to get to towns 1 and 2, since they cannot go
backwards on the plane from 1 to 3.
Ideas :
There is the shortest path with negative weight
SPFA + SLF You can get stuck !
SPFA + SLF The board is as follows :
void SPFA()
{
deque<ll > q;
d[s] = 0;
q.push_back(s);
vis[s] = 1;
while(!q.empty())
{
ll x = q.front();
q.pop_front();
vis[x] = 0;
for(int i=head[x]; i!=-1; i = nxt[i])
{
ll v = pnt[i];
if(d[v] > d[x] + cost[i] )
{
d[v] = d[x] + cost[i] ;
if(vis[v]) continue;
vis[v] = 1;
if(q.size()&&d[v]<d[q.front()]) //SLF, Small team leader , Big to the end of the team
q.push_front(v);
else
q.push_back(v);
}
}
}
}
AC The code is as follows :
#include <bits/stdc++.h>
#define buff \ ios::sync_with_stdio(false); \ cin.tie(0);
//#define int long long
using namespace std;
const int N = 1e6 + 9;
int n, m1, m2, gog;
int h[N], e[N], ne[N], w[N], idx;
int dist[N];
bool vis[N];
void add(int a, int b, int c)
{
idx++;
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx;
}
void spfa(int goal)
{
deque<int> q;
for (int i = 1; i <= n; i++)
dist[i] = 0x3f3f3f3f, vis[i] = 0;
q.push_back(gog);
dist[gog] = 0, vis[gog] = 1;
while (!q.empty())
{
int t = q.front();
q.pop_front();
vis[t] = 0;
for (int i = h[t]; i; i = ne[i])
{
int j = e[i];
if (dist[j] > dist[t] + w[i])
{
dist[j] = dist[t] + w[i];
if (vis[j])
continue;
vis[j] = 1;
if (q.size() && dist[j] < dist[q.front()])
q.push_front(j);
else
q.push_back(j);
}
}
}
}
void solve()
{
cin >> n >> m1 >> m2 >> gog;
int u, v, w;
for (int i = 1; i <= m1; i++)
{
cin >> u >> v >> w;
add(u, v, w);
add(v, u, w);
}
for (int i = 1; i <= m2; i++)
{
cin >> u >> v >> w;
add(u, v, w);
}
spfa(gog);
for (int i = 1; i <= n; i++)
{
if (dist[i] == 0x3f3f3f3f)
// puts("NO PATH");
cout << "NO PATH" << '\n';
else
cout << dist[i] << '\n';
}
}
int main()
{
buff;
solve();
}
边栏推荐
- [Blue Bridge Cup] children's worship circle
- Qt新项目_MyNotepad++
- Solve "sub number integer", "jump happily", "turn on the light"
- What are eNB, EPC and PGW?
- Qt-制作一个简单的计算器-实现四则运算-将结果以对话框的形式弹出来
- 693. 行程排序(map + 拓扑)
- Node.js通过ODBC访问PostgreSQL数据库
- mac(macos Monterey12.2 m1) 个人使用php开发
- rxjs Observable 自定义 Operator 的开发技巧
- The xftp connection Haikang camera reported an error: the SFTP subsystem application has been rejected. Please ensure that the SFTP subsystem settings of the SSH connection are valid
猜你喜欢

Skillfully use SSH to get through the Internet restrictions

How to modify the error of easydss on demand service sharing time?

题解:《你的飞碟在这儿》、《哥德巴赫猜想》

Don't spend money, spend an hour to build your own blog website

诚邀青年创作者,一起在元宇宙里与投资人、创业者交流人生如何做选择……...

为什么switch 的default后面要跟break?

Essential for operation and maintenance - Elk log analysis system

Partner cloud form strong upgrade! Pro version, more extraordinary!

Pointer from entry to advanced (1)

中文姓名提取(玩具代码——准头太小,权当玩闹)
随机推荐
你的 Sleep 服务会梦到服务网格外的 bookinfo 吗
Unity skframework framework (XII), score scoring module
Independent and controllable 3D cloud CAD: crowncad enables innovative design of enterprises
三翼鸟两周年:羽翼渐丰,腾飞指日可待
为什么switch 的default后面要跟break?
Performance optimization of memory function
We sincerely invite young creators to share with investors and entrepreneurs how to make choices in life in the metauniverse
linux下清理系统缓存并释放内存
基于ssm+jsp框架实现的学生选课信息管理系统【源码+数据库】
The xftp connection Haikang camera reported an error: the SFTP subsystem application has been rejected. Please ensure that the SFTP subsystem settings of the SSH connection are valid
【蓝桥杯选拔赛真题43】Scratch航天飞行 少儿编程scratch蓝桥杯选拔赛真题讲解
Skillfully use SSH to get through the Internet restrictions
Mysql常用命令详细大全
Unity skframework framework (XIX), POI points of interest / information points
693. 行程排序(map + 拓扑)
文件的下载与图片的预览
伙伴云表格强势升级!Pro版,更非凡!
Operation tutorial: how does easydss convert MP4 on demand files into RTSP video streams?
TVOC, VOC, VOCs gas detection + Solution
Find love for speed in F1 delta time Grand Prix