标签:des style blog http io ar color os sp
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1721 Accepted Submission(s):
431
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 const int ms=25; 8 const int inf=0xffffff; 9 struct edge 10 { 11 int u,v,w,next; 12 }edges[ms*ms]; 13 int head[ms],price[ms],dis[ms],cnt,n,m; 14 bool vis[ms]; 15 void add_edge(int u,int v,int w) 16 { 17 edges[cnt].u=u; 18 edges[cnt].v=v; 19 edges[cnt].w=w; 20 edges[cnt].next=head[u]; 21 head[u]=cnt++; 22 return ; 23 } 24 25 void input() 26 { 27 int i,j,id,pri; 28 memset(vis,false,sizeof(vis)); 29 memset(head,-1,sizeof(head)); 30 fill(dis,dis+ms,inf); 31 cnt=0; 32 scanf("%d",&n); 33 for(i=0;i<n;i++) 34 { 35 scanf("%d %d",&id,&pri); 36 price[id]=pri; 37 add_edge(0,id,pri-1); 38 } 39 for(i=1;i<=n;i++) 40 for(j=i+1;j<=n;j++) 41 if(price[i]==price[j]) 42 { 43 44 add_edge(i,j,0); 45 add_edge(j,i,0); 46 } 47 scanf("%d",&m); 48 while(m--) 49 { 50 scanf("%d %d %d",&i,&j,&pri); 51 add_edge(i,j,pri); 52 } 53 return ; 54 } 55 void spfa() 56 { 57 int i,s=0,to; 58 queue<int> que; 59 dis[s]=0; 60 que.push(s); 61 vis[s]=true; 62 while(!que.empty()) 63 { 64 // s=que.top(); 栈 65 s=que.front(); 66 que.pop(); 67 for(i=head[s];i!=-1;i=edges[i].next) 68 { 69 to=edges[i].v; 70 if(dis[to]>dis[s]+edges[i].w) 71 { 72 dis[to]=dis[s]+edges[i].w; 73 if(!vis[to]) 74 { 75 vis[to]=true; 76 que.push(to); 77 } 78 } 79 } 80 vis[s]=false; 81 } 82 return ; 83 } 84 void solve() 85 { 86 int i,j,k,ans=0; 87 spfa(); 88 for(i=1;i<=n;i++) 89 printf("%d %d\n",i,dis[i]); 90 bool flag; 91 for(i=1;i<=n;i++) 92 for(flag=true,j=1;j<=n&&flag;j++) 93 for(k=j+1;k<=n&&flag;k++) 94 if(i!=j&&i!=k) 95 { 96 if(dis[i]==dis[j]+dis[k]) 97 { 98 ans++; 99 flag=false; 100 } 101 } 102 printf("%d\n",ans); 103 return ; 104 } 105 int main() 106 { 107 int T; 108 scanf("%d",&T); 109 while(T--) 110 { 111 input(); 112 solve(); 113 } 114 return 0; 115 }
标签:des style blog http io ar color os sp
原文地址:http://www.cnblogs.com/767355675hutaishi/p/4147612.html