标签:color pen pac height test 图片 i++ span play
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int mac=4e5+50; 4 int head[mac],vis[mac],dis[mac]; 5 int n,m,num,i,j,t; 6 struct edge{ 7 int v,w,next; 8 }a[mac]; 9 void sc(){ 10 int x,y,z; 11 scanf("%d %d",&n,&m); 12 for(i=1;i<=m;i++){ 13 scanf("%d %d %d",&x,&y,&z); 14 a[++num]=(edge){y,z,head[x]}; 15 head[x]=num; 16 a[++num]=(edge){x,z,head[y]};//无向图 双向边,然后数组两倍边数 17 head[y]=num; 18 } 19 } 20 void spfa(){ 21 memset(dis,0x3f, sizeof(dis)); 22 deque<int>q; 23 q.push_back(1); 24 vis[1]=1; 25 dis[1]=0; 26 while(!q.empty()){ 27 t=q.front(); 28 q.pop_front(); 29 vis[t]=0; 30 for(j=head[t];j!=0;j=a[j].next){ 31 int v=a[j].v; 32 if(dis[v]>dis[t]+a[j].w){ 33 dis[v]=dis[t]+a[j].w; 34 if(!vis[v]) 35 { 36 vis[v]=1; 37 if(dis[v]<=dis[q.front()]) 38 q.push_front(v); 39 else 40 q.push_back(v); 41 } 42 } 43 } 44 } 45 if(dis[n]!=0x3f3f3f3f) 46 printf("%d\n",dis[n]); 47 else 48 cout<<"qwb baka"<<endl;//1和n不连通 49 } 50 void spfabest(){ 51 memset(dis, 0x3f, sizeof(dis)); 52 deque<int> q; 53 q.push_back(1); 54 vis[1] = 1; 55 dis[1] = 0; 56 int cnt = 1,sum=0; 57 while (!q.empty()) 58 { 59 t = q.front(); 60 // cout<<"t="<<t<<endl; 61 // while (cnt*dis[t] > sum) 62 // { 63 // q.pop_front(); 64 // q.push_back(t); 65 // t = q.front(); 66 // }把这个LLL优化屏蔽掉就过了 67 while (cnt*dis[t] > sum) 68 { 69 q.pop_front(); 70 q.push_back(t); 71 t = q.front(); 72 } 73 q.pop_front(); 74 cnt--; 75 sum-=dis[t]; 76 vis[t] = 0; 77 for(j=head[t];j!=0;j=a[j].next){ 78 //cout<<"j="<<j<<endl; 79 int v=a[j].v; 80 if(dis[v]>dis[t]+a[j].w){ 81 dis[v]=dis[t]+a[j].w; 82 if(!vis[v]) 83 { 84 vis[v]=1; 85 if(dis[v]<=dis[q.front()]){ 86 q.push_front(v); 87 } 88 else 89 q.push_back(v); 90 sum += dis[v]; 91 cnt++; 92 } 93 } 94 //printf("sum=%d,cnt=%d\n",sum,cnt); 95 } 96 } 97 if(dis[n]!=0x3f3f3f3f) 98 printf("%d\n",dis[n]); 99 else 100 cout<<"qwb baka"<<endl;//1和n不连通 101 } 102 int main(){ 103 sc(); 104 spfabest(); 105 } 106
先上题目链接 https://ac.nowcoder.com/acm/contest/96/E
emmm,以后写博客一定贴题目链接,提高食用效果。
标签:color pen pac height test 图片 i++ span play
原文地址:https://www.cnblogs.com/hcl6/p/13919865.html