<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      分層圖最短路

      分層圖

      分層圖確實(shí)是個(gè)不難的東西我也確實(shí)是一個(gè)菜雞...

      簡(jiǎn)要介紹

      分層圖常在圖論問(wèn)題中出現(xiàn)可任選K條邊更改邊權(quán)(變?yōu)?或是原來(lái)一半等)時(shí)應(yīng)用。

      分層圖,就是在應(yīng)對(duì)這種問(wèn)題時(shí),把原圖復(fù)制K層(總共K+1層)

      每相鄰層之間用更改邊權(quán)后的邊連接對(duì)應(yīng)的點(diǎn)(代表進(jìn)行一次操作)

      然后就可以在這個(gè)新圖中處理,得到問(wèn)題的最優(yōu)解

      圖例如下

      因?yàn)檫B接兩層的邊代表的是進(jìn)行了一次不可逆的邊權(quán)操作,所以都是有向邊

      每層圖內(nèi)部的邊就是和原圖一樣了

      簡(jiǎn)單說(shuō)明

      可能有人會(huì)有疑問(wèn),把圖復(fù)制K層后,空間和時(shí)間不會(huì)爆炸嘛?

      雖然分層圖總共有K+1層圖

      但因?yàn)槊繉佣贾皇菃渭兊膹?fù)制原圖

      所以內(nèi)容都是一樣的,一般沒(méi)有必要新建圖

      我們只需要用一個(gè)二維數(shù)組表示點(diǎn)編號(hào)及其所在層數(shù)

      例如在求最短路時(shí),用 dis[x][k] 表示分層圖中在k層的x點(diǎn)的最短距離

      即原圖中進(jìn)行了k次邊權(quán)操作的 x 點(diǎn)最短距離

      這樣就不需要新建圖了

      (如果最后還是炸了,說(shuō)明正解大概不是分層圖)

       

      例題

      題目描述

      Your are given an undirect connected graph.Every edge has a cost to pass.You should choose a path from S to T and you need to pay for all the edges in your path. However, you can choose at most k edges in the graph and change their costs to zero in the beginning. Please answer the minimal total cost you need to pay.
      您將獲得一個(gè)無(wú)向連通圖。每條邊都有成本要通過(guò)。您應(yīng)該選擇一條從S到T的路徑,并且需要為路徑中的所有邊付費(fèi)。但是,您可以在圖中選擇最多k個(gè)邊,并在開(kāi)始時(shí)將其成本更改為零。請(qǐng)回答您需要支付的最低總費(fèi)用。(機(jī)翻)

      輸入描述:

      The first line contains five integers n,m,S,T,K.
      For each of the following m lines, there are three integers a,b,l, meaning there is an edge that costs l between a and b.
      n is the number of nodes and m is the number of edges.
      第一行包含五個(gè)整數(shù)n,m,S,T,K。
      對(duì)于以下m行中的每一行,有三個(gè)整數(shù)a,b,l,這意味著在a和b之間存在成本為l的邊。
      n是節(jié)點(diǎn)數(shù),m是邊數(shù)。

      輸出描述:

      An integer meaning the minimal total cost.
      一個(gè)整數(shù)表示最小總成本。

      示例1

      輸入

      3 2 1 3 1
      1 2 1
      2 3 2

      輸出

      1

      備注:

      1≤n,m≤1e3,1≤S,T,a,b≤n,0≤k≤m,1≤l≤1e6
      Multiple edges and self loops are allowed.
      來(lái)源:牛客網(wǎng)暑期多校第四場(chǎng)J題

      #include <iostream>
      #include <cstdio>
      #include <cstring>
      #include <string>
      #include <queue>
      using namespace std;
      #define M 1000100
      struct node {
          int to,next,w;
      }e[2*M];
      typedef pair <int,int> p;
      priority_queue <p,vector<p>,greater<p> > q; 
      int dis[1010][1010],head[M],s,t,n,m,k=0;
      int path[1010],cnt;
      bool vis[1010][1010];
      void add(int u,int v,int w) {
          e[k].to=v; e[k].next=head[u]; 
          e[k].w = w; head[u]=k++;
      }
      void dij() {
          for (int i=1;i<=n;i++) {
              for (int j=0;j<=k;j++)
                  dis[i][j] = 0x7f7f7f7f;
          }
          memset(vis,0,sizeof vis);
          int now,son,x;
          dis[s][0] = 0;
          q.push(make_pair(0,s));
          while ( q.size() ) {
              now = q.top().second;
              q.pop();
              if ( now%n == 0 ) 
                  x = now/n-1,now=n;
              else x=now/n,now%=n;
              if ( vis[now][x] ) continue;
              vis[now][x] = 1;
              for (int i=head[now];~i;i=e[i].next) {
                  son = e[i].to;
                  if ( dis[now][x]+e[i].w<dis[son][x]) {
                      dis[son][x] = dis[now][x]+e[i].w;
                      q.push(make_pair(dis[son][x],son+n*x));
                  }
                  if ( x != cnt ) {
                      if ( dis[son][x+1] > dis[now][x]) {
                          dis[son][x+1] = dis[now][x];
                          q.push(make_pair(dis[son][x+1],son+x*n+n));
                      }
                  }
              }
          }
      }
      int main() {
          int a,b,c;
          memset(head,-1,sizeof head);
          scanf("%d%d%d%d%d",&n,&m,&s,&t,&cnt);
          for (int i=1;i<=m;i++) {
              scanf("%d%d%d",&a,&b,&c);
              add(a,b,c);
              add(b,a,c);
          }
          dij();
          printf("%d\n",dis[t][cnt]);
          return 0;
      }
      Dij堆優(yōu)化+分層圖
      
      

       

       
      posted @ 2019-07-29 14:47  Jachin毅軒  閱讀(662)  評(píng)論(0)    收藏  舉報(bào)
      主站蜘蛛池模板: 真实国产熟睡乱子伦视频| 亚洲一区二区av免费| 日韩视频中文字幕精品偷拍| 日韩欧美精品suv| 中国熟妇毛多多裸交视频| 国内精品极品久久免费看| 痉挛高潮喷水av无码免费| 少妇被多人c夜夜爽爽av| 国产激情一区二区三区在线| 中年国产丰满熟女乱子正在播放| 免费A级毛片无码A∨蜜芽试看 | 福利成人午夜国产一区| 欧美乱妇高清无乱码免费| 国内女人喷潮完整视频| 91久久亚洲综合精品成人| 亚洲区一区二区三区亚洲| 久久人体视频| 国产偷国产偷亚洲高清午夜| 亚洲成在人线AV品善网好看| 石棉县| 国产日韩精品视频无码| 高清国产av一区二区三区| 国产国亚洲洲人成人人专区| 久久99精品国产99久久6尤物| 亚洲aⅴ无码专区在线观看q| 国产一级精品在线免费看| 国产精品成人av在线观看春天| 亚洲综合不卡一区二区三区| 国产精品午夜av福利| 景谷| 久久精品国产久精国产| 十八禁在线观看视频播放免费 | 377P欧洲日本亚洲大胆| 国产va在线观看免费| 国产精品亚洲专区无码破解版| 久久亚洲国产精品久久| 久久精品一本到99热免费| 久久这里都是精品二| 亚洲理论电影在线观看| 一区二区中文字幕久久| 国产69精品久久久久乱码免费|