首页 > 其他分享 >5098: Sweet Butter spfa

5098: Sweet Butter spfa

时间:2023-03-30 18:11:24浏览次数:60  
标签:5098 pastures cow int Sweet sugar -- spfa pasture

描述

 

 

Farmer John has discovered the secret to making the sweetest butter in all of Wisconsin: sugar. By placing a sugar cube out in the pastures, he knows the N (1 <= N <= 500) cows will lick it and thus will produce super-sweet butter which can be marketed at better prices. Of course, he spends the extra money on luxuries for the cows.

FJ is a sly farmer. Like Pavlov of old, he knows he can train the cows to go to a certain pasture when they hear a bell. He intends to put the sugar there and then ring the bell in the middle of the afternoon so that the evening's milking produces perfect milk.

FJ knows each cow spends her time in a given pasture (not necessarily alone). Given the pasture location of the cows and a description of the paths that connect the pastures, find the pasture in which to place the sugar cube so that the total distance walked by the cows when FJ rings the bell is minimized. FJ knows the fields are connected well enough that some solution is always possible.

 

 

输入

 

 

  • Line 1: Three space-separated integers: N, the number of pastures: P (2 <= P <= 800), and the number of connecting paths: C (1 <= C <= 1,450). Cows are uniquely numbered 1..N. Pastures are uniquely numbered 1..P.

  • Lines 2..N+1: Each line contains a single integer that is the pasture number in which a cow is grazing. Cow i's pasture is listed on line i+1.

  • Lines N+2..N+C+1: Each line contains three space-separated integers that describe a single path that connects a pair of pastures and its length. Paths may be traversed in either direction. No pair of pastures is directly connected by more than one path. The first two integers are in the range 1..P; the third integer is in the range (1..225).

 

 

输出

 

 

  • Line 1: A single integer that is the minimum distance the cows must walk to a pasture with a sugar cube.

 

 

 

样例输入

 

3 4 5
2
3
4
1 2 1
1 3 5
2 3 7
2 4 3
3 4 5

样例输出

 8

提示

 

INPUT DETAILS:

This diagram shows the connections geometrically:

          P2  
 P1 @--1--@ C1
     \    |\
      \   | \
       5  7  3
        \ |   \
         \|    \ C3
       C2 @--5--@
          P3    P4

OUTPUT DETAILS: 

Putting the cube in pasture 4 means: cow 1 walks 3 units; cow 2 walks 5 units; cow 3 walks 0 units -- a total of 8.

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int inf = 1e6;
 4 struct node{
 5     int v,w;
 6 };
 7 int num[1005]; //记录第i个牧场奶牛的个数
 8 int dis[1005],vis[3000],ans; //dis起点到i的最短距离
 9 int n,p,c; 
10 vector<pair<int,int> > g[3005];
11 void spfa(int st)
12 {
13     memset(vis,0,sizeof(vis));
14     for(int i=1;i<=p;i++)
15     {
16         dis[i] = inf;
17     }
18     dis[st] = 0;vis[st] = 1;
19     queue<int> q;
20     q.push(st);
21     while(!q.empty())
22     {
23         int x = q.front();q.pop();
24         vis[x] = 0;
25         for(int i=0;i<g[x].size();i++)
26         {
27             int v = g[x][i].first,w = g[x][i].second;
28             if(dis[x]+w<dis[v])
29             {
30                 dis[v] = dis[x]+w;
31                 if(!vis[v])
32                 {
33                     vis[v] = 1;
34                     q.push(v);
35                 }
36             }
37         }
38     } 
39 }
40 int main()
41 {
42     cin>>n>>p>>c;
43     for(int i=1;i<=n;i++)
44     {
45         int x;cin>>x;num[x]++; 
46     } 
47     for(int i=1;i<=c;i++)
48     {
49         int x,y,z;cin>>x>>y>>z;
50         g[x].push_back(make_pair(y,z));
51         g[y].push_back(make_pair(x,z));
52     }
53     ans = 0x3f3f3f3f;
54     for(int i=1;i<=c;i++)
55     {
56         spfa(i);
57         int tmp = 0;
58         for(int i=1;i<=p;i++)
59             tmp += num[i]*dis[i];
60         ans = min(ans,tmp);
61     }
62     cout<<ans;
63      return 0;
64 }

 

标签:5098,pastures,cow,int,Sweet,sugar,--,spfa,pasture
From: https://www.cnblogs.com/jyssh/p/17273902.html

相关文章

  • Sweet Claw:最奇最难的三消游戏之一
    三消类游戏相信不少人都很熟悉,本作是一款很有创意的三消游戏,其匹配规则可以说前所未见,绝对考验玩家的逻辑思维能力,究竟这样的匹配规则创新靠不靠谱?看看就知道了。   游戏......
  • 最小费用最大流( spfa )
     constintN=5005,M=1e5+100;#defineinf1e9intall=1,hd[N],go[M],w[M],nxt[M],cost[M];intS,T,n,m;intpre[N],mn[N],dis[N],vis[N],ans=0;void......
  • SPFA
    importjava.util.Arrays;importjava.util.LinkedList;importjava.util.Queue;publicclassSPFA{ publicstaticvoidmain(String[]args){ } //边存......
  • Bellman_ford和spfa算法
    Bellman_ford算法bellman_ford算法在要求起点到终点存在负权边,要求在指定k步(这是spfa无法替代的)bellman_ford和spfa都可以判断图中有无负权环......
  • SweetAlert
    一:简介1.什么是SweetAlert?SweetAlert是可以替代Javascript原生的alert和confirm等函数呈现的弹出提示框,它将提示框进行了美化,并且允许自定义,支持设置提示框标题、提示......
  • J - 【黄色】这题真的是模板题 Gym - 102072J 【 SPFA 】
    J-【黄色】这题真的是模板题 Gym-102072J 在看完其他出题人出的毒瘤题之后,良心出题人终于看不下去了,决定出一道模板题来送给大家一个AC,那么,你们能不能接住这个......
  • 【Luogu3371】【模板】单源最短路径(SPFA)
    problem给出一个有向图求从某一点出发到所有点的最短路solutionSPFAcodes#include<iostream>#include<queue>#include<cstring>#definemaxn10010#definem......
  • hdu-1874-畅通工程续(dijkstra + SPFA )
    畅通工程续TimeLimit:3000/1000MS(Java/Others)    MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):36928    AcceptedSubmission(s):13......
  • 算法之Dijkstra及其堆优化和SPFA:图上单源最短路径神器
    签到题……题目传送门SPFA算法本人曾经写过一篇有关Bellman-ford的博,但就算是挂了优化的ford也只能过这道题的弱化版。今天就先填个坑,先讲SPFA。在这里我直接认为你们......
  • 算法之SPFA的前置:Bellman-Ford算法
    SPFA我们都知道一个叫SPFA的算法,它是用来计算单源最短路径的,但是,众所周知它不是很稳定,容易退化。SPFA是基于什么被提出的?基于一个叫做Bellman-Ford的算法。Bellman-For......