路徑方案數 [SPFA,拓撲排序]

來源:互聯網
上載者:User

標籤:正整數   main   lag   include   queue   stdin   輸出   void   const   

  路徑方案數[題目描述]

給一張無向圖,n 個點和 m 條邊,cyb 在 1 號點,他要去 2 號點, cyb 可以從 a 走到 b,若且唯若 a 到 2 的最短路,比 b 到 2 的最短路長。 求 cyb 的路徑方案數 兩條路徑不同,若且唯若將兩條路徑中依次經過的邊的編號不完全相同, 圖可能會有重邊; 由於答案可能很大, 只需要輸出答案對於 10^9+9 模數的值即可

[輸入檔案]

第一行兩個正整數 n,m 接下來 m 行 每行 x,y,z 表示有一條邊,長度為 z,連結了 x,y

[輸出檔案]

一個正整數表示答案

[輸入範例 1]

5 6

1 3 2

1 4 2

3 4 3

1 5 12

4 2 34

5 2 24

[輸出範例 1]

2

[輸入範例 2]

7 8

1 3 1

1 4 1

3 7 1

7 4 1

7 5 1

6 7 1

5 2 1

6 2 1

[輸出範例 2]

4

[資料範圍]

30%: N<=100,M<=1000

100%: N<=50000,,M<=100000

每條邊的長度<=1000

  分析:

  上午考試我是真的不知道在幹什麼,這個基本的最短路然後重建邊再拓撲也沒看出來。。。

  spfa以2為起點跑一遍,然後按照要求重建邊再跑拓撲即可。但是有一個問題,因為重建邊後1的入度可能不為0,那麼就反向建邊,因為可以保證2的入度一定為0。然後跑拓撲就行了。

  Code:

 

//It is made by HolseLee on 20th July 2018//mod #include<bits/stdc++.h>using namespace std;const int N=5e4+7;const int M=2e5+7;const int mod=1e9+9;int n,m,v[N],head[N],size;int dis[N],dg[N],ans;bool vis[N],rx[M];struct Node{  int to,next,val;}edge[M];inline int read(){  char ch=getchar();int num=0;bool flag=false;  while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)flag=true;ch=getchar();}  while(ch>=‘0‘&&ch<=‘9‘){num=num*10+ch-‘0‘;ch=getchar();}  return flag?-num:num;}inline void add(int x,int y,int z){  edge[++size].to=y;  edge[size].val=z;  edge[size].next=head[x];  head[x]=size;}void spfa(int sta){  memset(dis,0x7f,sizeof(dis));  memset(vis,false,sizeof(vis));  queue<int>t;t.push(sta);  dis[sta]=0;vis[sta]=true;  while(!t.empty()){    int x=t.front();t.pop();vis[x]=false;    for(int i=head[x];i!=-1;i=edge[i].next){      int y=edge[i].to;      if(dis[y]>dis[x]+edge[i].val){    dis[y]=dis[x]+edge[i].val;    if(!vis[y])t.push(y),vis[y]=true;      }    }  }}void remake(){  for(int x=1;x<=n;x++){    for(int i=head[x];i!=-1;i=edge[i].next){      int y=edge[i].to;      if(dis[x]<dis[y]){    rx[i]=true;dg[y]++;      }    }  }}void toplogic(int sta){  queue<int>t;t.push(sta);  memset(v,0,sizeof(v));  v[sta]=1;  while(!t.empty()){    int x=t.front();t.pop();    for(int i=head[x];i!=-1;i=edge[i].next){      if(rx[i]&&dg[edge[i].to]){    int y=edge[i].to;v[y]=(v[y]+v[x])%mod;--dg[y];    if(dg[y]==0)t.push(y);      }    }  }}int main(){  freopen("mod.in","r",stdin);  freopen("mod.out","w",stdout);  n=read();m=read();int x,y,z;  memset(head,-1,sizeof(head));  for(int i=1;i<=m;i++){    x=read();y=read();z=read();    add(x,y,z);add(y,x,z);}  spfa(2);remake();toplogic(2);  printf("%d\n",v[1]);  return 0;}

 

路徑方案數 [SPFA,拓撲排序]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.