当前位置:网站首页>香甜的黄油
香甜的黄油
2022-07-26 04:11:00 【Selvaggia】
spfa已死?
#include <iostream>
#include <queue>
using namespace std;
#define inf 0x3f3f3f3f
#define pii pair<int,int>
const int N=805;
const int M=1455;
int id[605];
struct edge{
int to,nxt,w;
}e[M<<1];
int head[N];
int cnt;
void add(int u,int v,int w){
e[++cnt].w=w;
e[cnt].to=v;
e[cnt].nxt=head[u];
head[u]=cnt;
}
int dist[N];
bool vis[N];
int n,p,m;//奶牛,农场,路
int spfa(int s){
for(int i=1;i<=p;i++){
dist[i]=inf;
vis[i]=false;
}
queue<int> Q;
Q.push(s);
dist[s]=0;
vis[s]=true;
while(!Q.empty()){
int t=Q.front();
Q.pop();vis[t]=false;
for(int i=head[t];i;i=e[i].nxt){
int to=e[i].to;
int w=e[i].w;
if(dist[to]>dist[t]+w){
dist[to]=dist[t]+w;
Q.push(to);
}
}
}
int res=0;
for(int i=0;i<n;i++){
if(dist[id[i]]==inf)return inf;
res+=dist[id[i]];
}
return res;
}
int dijikstra(int s){
for(int i=1;i<=p;i++){
dist[i]=inf;
vis[i]=false;
}
priority_queue<pii, vector<pii> ,greater<pii> > Q;
Q.push({
0,s});
dist[s]=0;
for(int k=0;k<p;k++){
if(Q.empty())break;
pii t=Q.top(); Q.pop();
int v=t.second;
if(vis[v]){
k--;
continue;
}
vis[v]=1;
for(int i=head[v];i;i=e[i].nxt){
int to=e[i].to;
int w=e[i].w;
if(dist[to]>dist[v]+w){
dist[to]=dist[v]+w;
Q.push({
dist[to],to});
}
}
}
int res=0;
for(int i=0;i<n;i++){
if(dist[id[i]]==inf)return inf;
res+=dist[id[i]];
}
// cout<<res<<endl;
return res;
}
int main(int argc, char** argv) {
// int n,p,m;//奶牛,农场,路
cin>>n>>p>>m;
for(int i=0;i<n;i++)cin>>id[i];
int u,v,w;
while(m--){
cin>>u>>v>>w;
add(u,v,w);
add(v,u,w);
}
int res=inf;
for(int i=1;i<=p;i++)res=min(res,dijikstra(i));//所有奶牛到i的距离之和
cout<<res;
return 0;
}
稍稍改动id[x]到count[x]
通过下面这个样例发现的,没错,就是多了一个无法到达的牧场而已
3 5 5
2
3
4
1 2 1
1 3 5
2 3 7
2 4 3
3 4 5
#include <iostream>
#include <queue>
using namespace std;
#define inf 0x3f3f3f3f
#define pii pair<int,int>
const int N=805;
const int M=1455;
int count[N];
int id[605];
struct edge{
int to,nxt,w;
}e[M<<1];
int head[N];
int cnt;
void add(int u,int v,int w){
e[++cnt].w=w;
e[cnt].to=v;
e[cnt].nxt=head[u];
head[u]=cnt;
}
int dist[N];
bool vis[N];
int n,p,m;//奶牛,农场,路
//int spfa(int i){
//
// return 0;
//}
int dijikstra(int s){
for(int i=1;i<=p;i++){
dist[i]=inf;
vis[i]=false;
}
priority_queue<pii, vector<pii> ,greater<pii> > Q;
Q.push({
0,s});
dist[s]=0;
for(int k=0;k<p;k++){
if(Q.empty())break;
pii t=Q.top(); Q.pop();
int v=t.second;
if(vis[v]){
k--;
continue;
}
vis[v]=true;
for(int i=head[v];i;i=e[i].nxt){
int to=e[i].to;
int w=e[i].w;
if(dist[to]>dist[v]+w){
dist[to]=dist[v]+w;
Q.push({
dist[to],to});
}
}
}
int res=0;
// for(int i=0;i<n;i++){
// if(dist[id[i]]==inf)return inf;
// res+=dist[id[i]];
// }
// cout<<p<<endl;
// for(int i=1;i<=p;i++)cout<<dist[i]<<" "<<count[i]<<endl;
// cout<<"hhhhh"<<endl;
for(int i=1;i<=p;i++){
if(dist[i]==inf&&count[i])return inf;//害!!!万一这个点没有牛呢,不能直接返回inf
res+=dist[i]*count[i];
}
// cout<<res<<endl;
return res;
}
int main(int argc, char** argv) {
// int n,p,m;//奶牛,农场,路
cin>>n>>p>>m;
int x;
for(int i=0;i<n;i++){
cin>>x;
count[x]++;
}
int u,v,w;
while(m--){
cin>>u>>v>>w;
add(u,v,w);
add(v,u,w);
}
int res=inf;
for(int i=1;i<=p;i++)res=min(res,dijikstra(i));//所有奶牛到i的距离之和
cout<<res;
return 0;
}
边栏推荐
- 零售连锁门店收银系统源码管理商品分类的功能逻辑分享
- PHP method to find the location of session storage file
- Matrix and Gauss elimination [matrix multiplication, Gauss elimination, solving linear equations, solving determinants] the most detailed in the whole network, with examples and sister chapters of 130
- Luoda development - audio stream processing - AAC / loopbacktest as an example
- 2021 CIKM |GF-VAE: A Flow-based Variational Autoencoder for Molecule Generation
- 荐书丨《教育心理学》:送给明日教师的一本书~
- Trust sums two numbers
- The second article, which is still unfinished, will be introduced again, and continue to explain oracledb_ Exporter monitors Oracle, a very low intrusive monitoring scheme.
- Opencv learning notes - edge detection and Canny operator, Sobel operator, lapiacian operator, ScHARR filter
- When you try to delete all bad code in the program | daily anecdotes
猜你喜欢

This article takes you to graph transformers

Dracoo master

Acwing_12. 背包问题求具体方案_dp
![Acwing game 61 [End]](/img/e3/191f7d888fc8cced608d41ef837a92.png)
Acwing game 61 [End]

加班一周开发了报表系统,这个低代码免费IT报表神器太好用了

LeetCode:1184. 公交站间的距离————简单

Helloworld案例分析

(translation) timing of website flow chart and user flow chart

《opencv学习笔记》-- 边缘检测和canny算子、sobel算子、LapIacian 算子、scharr滤波器

dijango学习
随机推荐
《opencv学习笔记》-- 霍夫变换
Share | 2022 big data white paper of digital security industry (PDF attached)
【云原生】谈谈老牌消息中间件ActiveMQ的理解
Retail chain store cashier system source code management commodity classification function logic sharing
JS get some attributes of the object
Sorting and searching
Uniapp pit filling Tour
`Oi problem solving ` ` leetcode '2040. The k-th minor product of two ordered arrays
2022杭电多校 Bowcraft
ZK snark: about private key, ring signature, zkksp
《opencv学习笔记》-- 边缘检测和canny算子、sobel算子、LapIacian 算子、scharr滤波器
(翻译)网站流程图和用户流程图的使用时机
LeetCode:1184. 公交站间的距离————简单
匿名函数的作用
如何构建面向海量数据、高实时要求的企业级OLAP数据引擎?
redux
Firewall command simple operation
1311_ Hardware design_ Summary of ICT concept, application, advantages and disadvantages
(translation) the button position convention can strengthen the user's usage habits
Maximum average value of continuous interval