P2966 [USACO09DEC]牛收費路徑Cow Toll Paths

来源:http://www.cnblogs.com/zwfymqz/archive/2017/06/21/7060788.html
-Advertisement-
Play Games

題目描述 Like everyone else, FJ is always thinking up ways to increase his revenue. To this end, he has set up a series of tolls that the cows will pay wh ...


題目描述

Like everyone else, FJ is always thinking up ways to increase his revenue. To this end, he has set up a series of tolls that the cows will pay when they traverse the cowpaths throughout the farm.

The cows move from any of the N (1 <= N <= 250) pastures conveniently numbered 1..N to any other pasture over a set of M (1 <= M <= 10,000) bidirectional cowpaths that connect pairs of different pastures A_j and B_j (1 <= A_j <= N; 1 <= B_j <= N). FJ has assigned a toll L_j (1 <= L_j <= 100,000) to the path connecting pastures A_j and B_j.

While there may be multiple cowpaths connecting the same pair of pastures, a cowpath will never connect a pasture to itself. Best of all, a cow can always move from any one pasture to any other pasture by following some sequence of cowpaths.

In an act that can only be described as greedy, FJ has also assigned a toll C_i (1 <= C_i <= 100,000) to every pasture. The cost of moving from one pasture to some different pasture is the sum of the tolls for each of the cowpaths that were traversed plus a *single additional toll* that is the maximum of all the pasture tolls encountered along the way, including the initial and destination pastures.

The patient cows wish to investigate their options. They want you to write a program that accepts K (1 <= K <= 10,000) queries and outputs the minimum cost of trip specified by each query. Query i is a pair of numbers s_i and t_i (1 <= s_i <= N; 1 <= t_i <= N; s_i != t_i) specifying a starting and ending pasture.

Consider this example diagram with five pastures:

The 'edge toll' for the path from pasture 1 to pasture 2 is 3. Pasture 2's 'node toll' is 5.

To travel from pasture 1 to pasture 4, traverse pastures 1 to 3 to 5 to 4. This incurs an edge toll of 2+1+1=4 and a node toll of 4 (since pasture 5's toll is greatest), for a total cost of 4+4=8.

The best way to travel from pasture 2 to pasture 3 is to traverse pastures 2 to 5 to 3. This incurs an edge toll of 3+1=4 and a node toll of 5, for a total cost of 4+5=9.

跟所有人一樣,農夫約翰以著寧教我負天下牛,休叫天下牛負我的偉大精神,日日夜夜苦思生 財之道。為了發財,他設置了一系列的規章制度,使得任何一隻奶牛在農場中的道路行走,都 要向農夫約翰上交過路費。 農場中由N(1 <= N <= 250)片草地(標號為1到N),並且有M(1 <= M <= 10000)條 雙向道路連接草地A_j和B_j(1 <= A_j <= N; 1 <= B_j <= N)。

奶牛們從任意一片草 地出發可以抵達任意一片的草地。FJ已經在連接A_j和B_j的雙向道路上設置一個過路費L_j (1 <= L_j <= 100,000)。 可能有多條道路連接相同的兩片草地,但是不存在一條道路連接一片草地和這片草地本身。最 值得慶幸的是,奶牛從任意一篇草地出發,經過一系列的路徑,總是可以抵達其它的任意一片 草地。 除了貪得無厭,叫獸都不知道該說什麼好。

FJ竟然在每片草地上面也設置了一個過路費C_i (1 <= C_i <= 100000)。從一片草地到另外一片草地的費用,是經過的所有道路的過路 費之和,加上經過的所有的草地(包括起點和終點)的過路費的最大值。 任勞任怨的牛們希望去調查一下她們應該選擇那一條路徑。

她們要你寫一個程式,接受K(1 <= K <= 10,000)個問題並且輸出每個詢問對應的最小花費。第i個問題包含兩個數字s_i 和t_i(1 <= s_i <= N; 1 <= t_i <= N; s_i != t_i),表示起點和終點的草地。

輸入輸出格式

輸入格式:
  • Line 1: Three space separated integers: N, M, and K

  • Lines 2..N+1: Line i+1 contains a single integer: C_i

  • Lines N+2..N+M+1: Line j+N+1 contains three space separated

integers: A_j, B_j, and L_j

  • Lines N+M+2..N+M+K+1: Line i+N+M+1 specifies query i using two space-separated integers: s_i and t_i
輸出格式:
  • Lines 1..K: Line i contains a single integer which is the lowest cost of any route from s_i to t_i

輸入輸出樣例

輸入樣例#1:
5 7 2 
2 
5 
3 
3 
4 
1 2 3 
1 3 2 
2 5 3 
5 3 1 
5 4 1 
2 4 3 
3 4 4 
1 4 
2 3 
輸出樣例#1:
8 
9 


給大家介紹一種不用排序就能AC的方法

(排序的方法請看這位大神的博客:http://www.cnblogs.com/peter-le/p/6014643.html

我們可以分別記錄下從點x到點y 的最短路徑的長度和在最短路上的花費

然後就是套Floyd的模板。

但是註意,因為你的長度和花費是分開記錄的

所以一遍Floyd跑出來的不一定是最小值

我們可以多跑幾遍試試

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 using namespace std;
 6 const int MAXN=251;
 7 const int maxn=0x7ffffff;
 8 void read(int & n)
 9 {
10     char c='+';int x=0;    
11     while(c<'0'||c>'9')c=getchar();
12     while(c>='0'&&c<='9')
13     {
14         x=x*10+c-48;
15         c=getchar();
16     }
17     n=x;
18     
19 }
20 int a[MAXN][MAXN];
21 int spend[MAXN][MAXN];
22 int b[MAXN];
23 int n,m,q;
24 void floyd()
25 {
26     for(int k=1;k<=n;k++)
27         for(int i=1;i<=n;i++)
28             for(int j=1;j<=n;j++)
29             {
30                 int p=a[i][k]+a[k][j]+max(spend[i][k],spend[k][j]);
31                 if((p<a[i][j]+spend[i][j])&&a[i][k]<maxn&&a[k][j]<maxn)
32                 {
33                     //a[i][j]=a[i][j]-max(spend[i],spend[j]);
34                     a[i][j]=a[i][k]+a[k][j];
35                     spend[i][j]=max(spend[i][k],spend[k][j]);
36                 }            
37             }
38 }
39 int main()
40 {
41     read(n);read(m);read(q);
42     for(int i=1;i<=n;i++)
43         read(b[i]);
44     for(int i=1;i<=n;i++)
45         for(int j=1;j<=n;j++)
46         {
47             if(i==j)
48             a[i][j]=0;
49             else
50             a[i][j]=maxn;
51         }
52             
53     for(int i=1;i<=m;i++)
54     {
55         int x,y,z;
56         read(x);read(y);read(z);
57         if(a[x][y]>z)
58         {
59             a[x][y]=z;
60             a[y][x]=z;
61             spend[x][y]=max(b[x],b[y]);
62             spend[y][x]=max(b[x],b[y]);
63         }
64     
65     }
66     floyd();
67     floyd();
68     floyd();            
69     for(int i=1;i<=q;i++)
70     {
71         int x,y;
72         read(x);read(y);
73         printf("%d\n",a[x][y]+spend[x][y]);
74     }
75     return 0;
76 }

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 這篇說說工廠的最後一種,還是先上 抽象工廠模式定義(來自百度百科): 抽象工廠模式是所有形態的工廠模式中最為抽象和最具一般性的一種形態。抽象工廠模式是指當有多個抽象角色時,使用的一種工廠模式。抽象工廠模式可以向客戶端提供一個介面,使客戶端在不必指定產品的具體的情況下,創建多個產品族中的產品對象。根據 ...
  • 今天,下午在和朋友聊天的時候,聊起了反射這個話題。 我們就從下麵這個段簡單的代碼開始吧。 這個代碼輸出什麼,想必大部分的讀者跟我一樣,會很快地知道答案:0 1 2 3 4 5 6 7 8 9。事實也是如此: 朋友這個時候就提出一個問題,你看在代碼裡面有一個aa();你有什麼辦法,實現這個aa函數,讓 ...
  • 題目: LeetCode: [13. Roman to Integer][1] 描述: 題意為 給定一個羅馬數字轉換為整數(1 3999)輸出。 分析: 代碼: c++ int romanToInt(string s) { int nRes = 0; int nLen = s.length(); f ...
  • 描述 題目和答案來自於 "阿裡雲大學 知乎專欄" 題目 1. 現在有如下一段代碼 將產生哪種結果: A. Compilation will fail B. Compilation will succeed and the program will print“0” C. Compilation wi ...
  • 1.collatz序列 編寫一個名為 collatz()的函數,它 有一個名為 number 的參數。如果參數是偶數, 那麼 collatz()就列印出 number // 2,並返回該值。如果 number 是奇數,collatz()就打 印並返回 3 * number + 1。 然後編寫一個程式 ...
  • 今天做了幾道String常見操作。先來幾個代碼實例: 例一:此方法,僅把字元串前後出現的空格去掉了,中間部分不會。 例二: 例三: 例四: 例五&例六: 在例五和例六中,可以從列印方式去思考,為什麼例六要不例五速度快那麼多。 例五的執行方式就好比: 例六的執行方式就不一樣了: 它是執行一次之後,下一 ...
  • 錯誤寫法 按照一般的方法return會報錯 需要使用 raise gen.Return(response.body) 代替return 官方例子 In Python 3.3, this exception is no longer necessary: the return statement ca ...
  • 兩者的區別主要集中以下幾個方面: 1.key是否允許為空 HashMap允許key為null,Hashtable不允許key為null。 2.value是否允許為空 HashMap允許value為空,Hashtbale不允許value為null。 3.線程是否安全 HashMap線程不安全,Hash ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...