PKU 3613 Cow Relays (指定路徑條數的最短路)

來源:互聯網
上載者:User

標籤:style   class   blog   code   2014   string   



題意:N,T,S,E:給你T條邊,每條邊兩端都有編號和權值,問從S走到E允許走N條邊,求最短路。


foyld加矩陣快速冪思想。


注意要把邊離散


#include <iostream>#include <fstream>#include <string.h>#include <algorithm>using namespace std;#define M 303#define inf 0x3fffffffstruct node{int a[M][M];node(){for(int i=0;i<M;i++){for(int j=0;j<M;j++)a[i][j]=inf;}}};int n,t,s,e;int mp[1010],cnt;node ma;void foyld(node &a,node &b,node &c){node temp;for(int k=1;k<=cnt;k++){for(int i=1;i<=cnt;i++){for(int j=1;j<=cnt;j++)temp.a[i][j]=min(a.a[i][k]+b.a[k][j],temp.a[i][j]);}}for(int i=0;i<M;i++)for(int j=0;j<M;j++)c.a[i][j]=temp.a[i][j];}void pow(){node ans;for(int i=0;i<M;i++) ans.a[i][i]=0;while(n){if(n&1)foyld(ma,ans,ans);foyld(ma,ma,ma);n>>=1;}printf("%d\n",ans.a[mp[s]][mp[e]]);}int main(){while(~scanf("%d%d%d%d",&n,&t,&s,&e)){int a,b,c;cnt=0;memset(mp,0,sizeof(mp));while(t--){scanf("%d%d%d",&c,&a,&b);if(!mp[a]) mp[a]=++cnt;if(!mp[b]) mp[b]=++cnt;ma.a[mp[a]][mp[b]]=ma.a[mp[b]][mp[a]]=c;}pow();}return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.