hdu 1535 Invitation Cards
2015-06-17 20:40
211 查看
#include<stdio.h> #include<string.h> #include<algorithm> #include<queue> using namespace std; const int inf=1<<24; const int N=1000000+5; struct node { int to,w; node* next; }; node* edge ; node* reedge ; int n,m,x,dist ,inq ,q ; void spfa(int flag) { int i,u,h=0,t=1; node* ptr; for(i=0; i<=n; i++) { dist[i]=inf; inq[i]=0; } dist[1]=0; inq[1]=1; q[0]=1; while(h!=t) { u=q[h]; h++; inq[u]=0; if(flag==1) ptr=edge[u]; else ptr=reedge[u]; while(ptr) { if(dist[ptr->to]>(dist[u]+ptr->w)) { dist[ptr->to]=dist[u]+ptr->w; if(inq[ptr->to]==0) { q[t]=ptr->to; t++; inq[ptr->to]=1; } } ptr=ptr->next; } } } int main() { int cas,i,u,v,w,ans; scanf("%d",&cas); while(cas--) { scanf("%d%d",&n,&m); for(i=1; i<=n; i++) { edge[i]=NULL; reedge[i]=NULL; } for(i=0; i<m; i++) { scanf("%d%d%d",&v,&u,&w); node *temp; temp= new node; temp->to=u; temp->w=w; temp->next=NULL; if(edge[v]==NULL) { edge[v]=temp; } else { temp->next=edge[v]; edge[v]=temp; } temp= new node; temp->to=v; temp->w=w; temp->next=NULL; if(reedge[u]==NULL) { reedge[u]=temp; } else { temp->next=reedge[u]; reedge[u]=temp; } } ans=0; spfa(1); for(i=2; i<=n; i++) { //printf("%d\n",dist[i]); ans+=dist[i]; } spfa(0); for(i=2; i<=n; i++) { ans+=dist[i]; } printf("%d\n",ans); } return 0; }
相关文章推荐
- 揭秘LocalBroadcastManager实现原理
- 最小生成树算法(2)------------Kruskal
- [D-VIII] 限位数对称制2整数 浮点数加减法器
- MySQL绿色版的安装(mysql-5.6.22-win32.zip)
- 第三次Sprint-最后冲刺
- 软件工程理论阅读笔记1
- WorldWind源码剖析系列:配置载入器类ConfigurationLoader
- Convolutional Neural Networks for Visual Recognition 2
- OpenMP笔记:入门
- ror笔记2
- python学习笔记
- Sort与Sorted的区别与联系
- 赋值兼容规则
- 8.4.6 从右到左布局(RTL Layout)
- The Titanium Command Line (CLI) project
- NPOIHelper
- 获取plist文件的全路径
- 《图书管理系统——java》
- 团队开发------第一次冲刺第4天
- 12 Integer to Roman(int转罗马数字Medium)