圖論-生成樹-黑暗城堡

沒什麼好說的,沒有想清楚結果把計算ans數組時候用到的鏈式向前星的head寫錯了。。WA得還剩20分。
應該要寫head[v[i].v]的,不能寫head[i],因爲現在的第i個位置上放的已經不是原來的第i個點了。

#include<bits/stdc++.h>
#define rep(i,l,r) for(int i=(l);i<=(r);i++)
#define per(i,r,l) for(int i=(r);i>=(l);i--)
#define random(l,r) ((l)+rand()%((r)-(l)+1))
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int inf=1e9+10,mod=2147483647,N=2222,M=2e6+10000;
const double eps=1e-6;
struct node{
	int v,dis;
	bool friend operator < (node a,node b){
		return a.dis<b.dis;
	}
}v[N];
int nxt[M],to[M],w[M],head[N],len;
int n,m,ans[N],pos[N];
bool vis[N];
ll anss=1;
inline void addedge(int a,int b,int ww){
	nxt[++len]=head[a]; head[a]=len; to[len]=b; w[len]=ww;
}
inline void Dijkstra(){
	rep(i,1,n) v[i].v=i,v[i].dis=inf;
	v[1].dis=0;
	rep(i,1,n-1){
		int now=inf,k=0;
		rep(j,1,n) if((vis[j]==false)&&(v[j].dis<now)) k=j,now=v[j].dis;
		vis[k]=true;
		for(int e=head[k];e!=0;e=nxt[e]) v[to[e]].dis=min(v[to[e]].dis,v[k].dis+w[e]);
	}
}
int main(){
	ios::sync_with_stdio(false); cin.tie(0);
	cin>>n>>m;
	rep(i,1,m){
		int a,b,w; cin>>a>>b>>w;
		addedge(a,b,w); addedge(b,a,w);
	}
	Dijkstra();
	//rep(i,1,n) printf("dis[%d]=%d\n",i,v[i].dis);
	sort(v+1,v+1+n);
	rep(i,1,n) pos[v[i].v]=i;
	//rep(i,1,n) printf("v[%d]=%d\n",i,v[i].v);
	ans[v[1].v]=1;
	rep(i,1,n){
		for(int e=head[v[i].v];e!=0;e=nxt[e]){
			if(v[pos[to[e]]].dis==v[i].dis+w[e]) ans[v[pos[to[e]]].v]++;
		}
	}
	rep(i,1,n) anss=(anss*(long long)(ans[i]>0?ans[i]:1))%mod;
	cout<<anss;
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章