1. 程式人生 > >Problem M. Walking Plan hdu6331(最短路)

Problem M. Walking Plan hdu6331(最短路)

Problem M. Walking Plan
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 375 Accepted Submission(s): 128

Problem Description
There are n intersections in Bytetown, connected with m one way streets. Little Q likes sport walking very much, he plans to walk for q days. On the i-th day, Little Q plans to start walking at the si-th intersection, walk through at least ki streets and finally return to the ti-th intersection.
Little Q’s smart phone will record his walking route. Compared to stay healthy, Little Q cares the statistics more. So he wants to minimize the total walking length of each day. Please write a program to help him find the best route.

Input
The first line of the input contains an integer T(1≤T≤10), denoting the number of test cases.
In each test case, there are 2 integers n,m(2≤n≤50,1≤m≤10000) in the first line, denoting the number of intersections and one way streets.
In the next m lines, each line contains 3 integers ui,vi,wi(1≤ui,vi≤n,ui≠vi,1≤wi≤10000), denoting a one way street from the intersection ui to vi, and the length of it is wi.
Then in the next line, there is an integer q(1≤q≤100000), denoting the number of days.
In the next q lines, each line contains 3 integers si,ti,ki(1≤si,ti≤n,1≤ki≤10000), describing the walking plan.

Output
For each walking plan, print a single line containing an integer, denoting the minimum total walking length. If there is no solution, please print -1.

Sample Input

2
3 3
1 2 1
2 3 10
3 1 100
3
1 1 1
1 2 1
1 3 1
2 1
1 2 1
1
2 1 1

Sample Output

111
1
11
-1

題意:每次查詢,求出u到v至少經過k條邊的最短路

思路:看了大神的思路,這題容易想到dp[i][j][k] 表示從i到j走了k條邊的最短路,轉移也很很簡單,floyd加多一維就行了,但是這裡的k有10000,顯然不行,但是我們可以定義個輔助陣列
dp2[i][j][k] 表示i到j走了k*100條路的最短路,轉移可以通過dp1來轉移,最後一點就是題目要求
最少k條路的最短路,那麼我們在定義一個dp3[i][j][k] 代表從i到i走了至少k條路的最短路,那麼轉移就是 dp3[i][j][k] = min(dp3[i][j][k],dp1[i][w][k]+dis[w][j]) dis為w到j的最短路

accode

#include<bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f3f3f3f3f
const int maxn = 1e7+4;
using namespace std;
LL mod;
LL dp1[52][52][100+4];
LL dp2[52][52][100+5];
LL dp3[52][52][100+42];
int T;
LL dis[55][55];
LL d[55][55];
int n,m;
int q;
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        memset(d,0x3f,sizeof(d));
        for(int i = 1; i<=m; i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            d[u][v] = min(d[u][v],(LL)w);
        }
        for(int i = 1; i<=n; i++)
        {
            for(int j = 1; j<=n; j++)
            {
                dis[i][j] = d[i][j];
            }
            dis[i][i] = 0;
        }
        for(int k = 1; k<=n; k++)
        {
            for(int i = 1; i<=n; i++)
            {
                for(int j = 1; j<=n; j++)
                {
                    dis[i][j] = min(dis[i][j],dis[i][k]+dis[k][j]);
                }
            }
        }

        memset(dp1,0x3f,sizeof(dp1));
        for(int i = 1;i<=n;i++){
            dp1[i][i][0] = 0;
        }
        for(int k = 1;k<=100;k++){
            for(int i = 1;i<=n;i++){
                for(int j = 1;j<=n;j++){
                    for(int z = 1;z<=n;z++){
                        dp1[j][z][k]=min(dp1[j][z][k],dp1[j][i][k-1]+d[i][z]);
                    }
                }
            }
        }

        memset(dp2,0x3f,sizeof(dp2));
        for(int i = 1;i<=n;i++){
            dp2[i][i][0] = 0;
        }
        for(int k = 1;k<=100;k++){
            for(int i = 1;i<=n;i++){
                for(int j = 1;j<=n;j++){
                    for(int z = 1;z<=n;z++){
                        dp2[j][z][k]=min(dp2[j][z][k],dp2[j][i][k-1]+dp1[i][z][100]);
                    }
                }
            }
        }

        memset(dp3,0x3f,sizeof(dp3));
        for(int k = 0;k<=100;k++){
            for(int i = 1;i<=n;i++){
                for(int j = 1;j<=n;j++){
                    for(int z = 1;z<=n;z++){
                        dp3[j][z][k]=min(dp3[j][z][k],dp1[j][i][k]+dis[i][z]);
                    }
                }
            }
        }

        scanf("%d",&q);
        while(q--){
            int u,v,k;
            scanf("%d%d%d",&u,&v,&k);
            int t1 = k/100;
            int t2 = k%100;
            LL ans = INF;
            for(int i = 1;i<=n;i++){
                ans = min(ans,dp2[u][i][t1]+dp3[i][v][t2]);
            }
            if(ans>=INF){
                puts("-1");
                continue;
            }
            printf("%lld\n",ans);
        }
    }
}


相關推薦

Problem M. Walking Plan hdu6331短路

Problem M. Walking Plan Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others) Total Submission(s)

HDU-6331 Problem M. Walking Plan(分塊+短路)

Problem M. Walking Plan Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 169   

2018多校第3場 M-Walking Plan && HDU6331 Problem M. Walking Plan

題意:給定一個 n 個點,m 條邊的有向圖,q 次詢問 s 到 t 經過至少 k 條邊的最短路 資料範圍:2 ≤ n ≤ 50,1 ≤ m,k ≤ 10000,1 ≤ q ≤ 100000 思路分析:分塊計算,k<=10000,把100條邊縮成一條邊。偷

短路第七屆福建省大學生程序設計競賽 Problem J- X

main tails return ros code and rect list def Problem Description X is a fully prosperous country, especially known for its complicated

網絡提速短路

三臺 oid size 註意 減少 組成 getchar() cst etc codevs——1243 網絡提速 時間限制: 1 s 空間限制: 128000 KB 題目等級 : 黃金 Gold

單源短路徑短路

ext getchar 路徑 鄰接鏈表 單源最短路 fin struct true com 洛谷——P3371 【模板】單源最短路徑(spfa) 題目描述 如題,給出一個有向圖,請輸出從某一點出發到所有點的最短路徑長度。 輸入輸出格式 輸入格

Currency Exchange短路

quest ber its 只需要 nging lars script end ive                            poj—— 1860 Currency Exchange Time Limit: 1000

Codeforces Gym 100269 Dwarf Tower 短路

pair iostream printf sync part ret scribe first pan 題目連接: http://codeforces.com/gym/100269/attachments Description Little Vasya is playin

[BZOJ2118] 墨墨的等式短路

getch val light ont etc 選擇 clu sdi con 傳送門 好神啊。。 需要用非負數個a1,a2,a3...an來湊出B 可以知道,如果一個數x能被湊出來,那麽x+a1,x+a2.......x+an也都能被湊出來 那麽我們只需要選擇

【Luogu1608】路徑統計短路DP

要花 include dijkstra main std mar 行為 不能 總數 題目傳送門 題目描述 “RP餐廳”的員工素質就是不一般,在齊刷刷的算出同一個電話號碼之後,就準備讓HZH,TZY去送快餐了,他們將自己居住的城市畫了一張地圖,已知在他們的地圖上,有N個地方,

【CodeForces954D】Fight Against Traffic短路

AC ces tin info sin span == ace com Description 題目鏈接 Solution 從起點和終點分別做一次最短路並記錄結果 枚舉每一條可能的邊判斷 Code #include <cstdio> #include <a

hdu 5521 Meeting短路

names n) can style air ima pid hid con 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=5521 題意:有1-n共n個點,給出m個塊(完全圖),並知道塊內各點之間互相到達花費時

51-node-1649齊頭並進短路

else Go oid != return string OS AC amp 題意:中文題,沒啥坑點; 解題思路:這道題一開始以為要跑兩個最短路,後來發現不用,因為如果給定了鐵路的線路,那麽,公路一定是n個節點無向圖的補圖,所以,鐵路和公路之間一定有一個是可以直接從1到n的

POJ-1860 Currency Exchange 短路

條件 n) fin inline AS 我們 fde 思想 %d https://vjudge.net/problem/POJ-1860 題意 有多種匯幣,匯幣之間可以交換,這需要手續費,當你用100A幣交換B幣時,A到B的匯率是29.75,手續費是0.39,那麽你可以

POJ-2253 Frogger短路

tor struct code namespace https href IV 所有 put https://vjudge.net/problem/POJ-2253 題意 公青蛙想到母青蛙那裏去,期間有許多石頭,公青蛙可以通過這些石頭跳過去。問至少要跳的最大距離,

CF37E Trial for Chief短路

mem aps ons stream can lose name oid 次數 題意 題意是給你一張 NMNMNM 的圖,每個點有黑色和白色,初始全為白色,每次可以把一個相同顏色的連續區域染色,求最少的染色次數;(n,m<=50) 題解 轉化為最短路。對於每一個點與它

一個人的旅行 HDU - 2066 短路

mis 一個人 ssi 輸出 int cstring col 個數 cto 一個人的旅行 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su

BZOJ1880: [Sdoi2009]Elaxia的路線短路

space def iostream bzoj -c 路線 ostream 要求 efi 題目描述 最近,Elaxia和w**的關系特別好,他們很想整天在一起,但是大學的學習太緊張了,他們 必須合理地安排兩個人在一起的時間。 Elaxia和w**每天都要奔波於宿舍和實驗

【BZOJ2118】墨墨的等式短路

urn lin pre get [1] 最短 bool 沒有 emp 【BZOJ2118】墨墨的等式(最短路) 題面 BZOJ 洛谷 題解 和跳樓機那題是一樣的。 只不過走的方式從\(3\)種變成了\(n\)種而已,其他的根本沒有區別了。 #include<iostr

[Code+#4]短路 短路

wiki sizeof 一道 什麽 code node urn 快捷 mem [Code+#4]最短路 題目背景 在北緯 91° ,有一個神奇的國度,叫做企鵝國。這裏的企鵝也有自己發達的文明,稱為企鵝文明。因為企鵝只有黑白兩種顏色,所以他們的數學也是以二進制為基礎發展的。