Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
我的过了2969ms……In Reply To:有人用vector+priority_queue过的么? Posted by:200730690105 at 2010-01-23 19:09:27 //我的过了 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<functional> using namespace std; typedef long long ll; typedef pair<ll,ll> P; const ll maxn=50000+10; const ll inf=1e15; ll e,v; vector<P> G[maxn]; ll w[maxn]; ll d[maxn]; void init(){ for(ll i=0;i<=v;++i) d[i]=inf; d[1]=0; } void dij(){ priority_queue<P,vector<P>,greater<P> > q; q.push(make_pair(0,1)); while(!q.empty()){ ll now=q.top().second; q.pop(); for(ll i=0;i<G[now].size();++i){ ll t=G[now][i].first; if(d[t]>d[now]+G[now][i].second){ d[t]=d[now]+G[now][i].second; q.push(make_pair(d[t],t)); } } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); ll T; cin>>T; while(T--){ memset(d,0,sizeof(d)); cin>>v>>e; for(ll i=1;i<=v;++i){ cin>>w[i]; G[i].clear(); } init(); for(ll i=1;i<=e;++i){ ll a,b,c; cin>>a>>b>>c; G[a].push_back(make_pair(b,c)); G[b].push_back(make_pair(a,c)); } dij(); ll ans=0; bool flag=false; for(ll i=2;i<=v;++i){ ans+=(d[i]*w[i]); if(d[i]>=inf){ flag=true; break; } } if(flag){ cout<<"No Answer"<<endl; continue; } cout<<ans<<endl; } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator