Invitation Cards http://poj.org/problem?id=1511
dij+priority queue o (elogv)
1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #define mt(a,b) memset(a,b,sizeof(a)) 5 using namespace std; 6 typedef __int64 LL; 7 const int inf=0x3f3f3f3f; 8 class Dijkstra { ///单源最短路 o(ME*log(MV)) 9 typedef int typec;///边权的类型 10 static const int ME=1000010;///边的个数 11 static const int MV=1000010;///点的个数 12 struct Q { 13 int id; 14 typec w; 15 friend bool operator <(const Q &a,const Q &b) { 16 return a.w>b.w; 17 } 18 } now; 19 priority_queue<Q> q; 20 struct E { 21 int v,next; 22 typec w; 23 } e[ME]; 24 int le,head[MV]; 25 typec dist[MV]; 26 bool used[MV]; 27 public: 28 void init() { 29 le=0; 30 mt(head,-1); 31 } 32 void add(int u,int v,typec w) { 33 e[le].v=v; 34 e[le].w=w; 35 e[le].next=head[u]; 36 head[u]=le++; 37 } 38 void solve(int s) { 39 for(int i=0; i<MV; i++) { 40 used[i]=true; 41 dist[i]=inf; 42 } 43 dist[s]=0; 44 now.id=s; 45 now.w=0; 46 while(!q.empty()) q.pop(); 47 q.push(now); 48 while(!q.empty()) { 49 now=q.top(); 50 q.pop(); 51 int u=now.id; 52 if(used[u]) { 53 used[u]=false; 54 for(int i=head[u]; ~i; i=e[i].next) { 55 int v=e[i].v; 56 typec w=e[i].w; 57 if(used[v]&&dist[v]>w+dist[u]) { 58 dist[v]=w+dist[u]; 59 now.id=v; 60 now.w=dist[v]; 61 q.push(now); 62 } 63 } 64 } 65 } 66 } 67 typec getdist(int id) { 68 return dist[id]; 69 } 70 } gx; 71 struct IN{ 72 int u,v,w; 73 }e[1000010]; 74 int main() { 75 int t,n,m,u,v,w; 76 while(~scanf("%d",&t)) { 77 while(t--) { 78 scanf("%d%d",&n,&m); 79 gx.init(); 80 for(int i=0;i<m;i++) { 81 scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w); 82 gx.add(e[i].u,e[i].v,e[i].w); 83 } 84 gx.solve(1); 85 LL ans=0; 86 for(int i=1; i<=n; i++) { 87 ans+=gx.getdist(i); 88 } 89 gx.init(); 90 for(int i=0;i<m;i++){ 91 gx.add(e[i].v,e[i].u,e[i].w); 92 } 93 gx.solve(1); 94 for(int i=1; i<=n; i++) { 95 ans+=gx.getdist(i); 96 } 97 printf("%I64d\n",ans); 98 } 99 } 100 return 0; 101 }
MPI Maelstrom http://poj.org/problem?id=1502
dij+priority queue o (elogv)
1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #include<stack> 5 #include<algorithm> 6 #define mt(a,b) memset(a,b,sizeof(a)) 7 using namespace std; 8 typedef __int64 LL; 9 const int inf=0x3f3f3f3f; 10 class Dijkstra { ///单源最短路 o(ME*log(MV)) 11 typedef int typec;///边权的类型 12 static const int ME=10010;///边的个数 13 static const int MV=110;///点的个数 14 struct Q { 15 int id; 16 typec w; 17 friend bool operator <(const Q &a,const Q &b) { 18 return a.w>b.w; 19 } 20 } now; 21 priority_queue<Q> q; 22 struct E { 23 int v,next; 24 typec w; 25 } e[ME]; 26 int le,head[MV]; 27 typec dist[MV]; 28 bool used[MV]; 29 public: 30 void init() { 31 le=0; 32 mt(head,-1); 33 } 34 void add(int u,int v,typec w) { 35 e[le].v=v; 36 e[le].w=w; 37 e[le].next=head[u]; 38 head[u]=le++; 39 } 40 void solve(int s) { 41 for(int i=0; i<MV; i++) { 42 used[i]=true; 43 dist[i]=inf; 44 } 45 dist[s]=0; 46 now.id=s; 47 now.w=0; 48 while(!q.empty()) q.pop(); 49 q.push(now); 50 while(!q.empty()) { 51 now=q.top(); 52 q.pop(); 53 int u=now.id; 54 if(used[u]) { 55 used[u]=false; 56 for(int i=head[u]; ~i; i=e[i].next) { 57 int v=e[i].v; 58 typec w=e[i].w; 59 if(used[v]&&dist[v]>w+dist[u]) { 60 dist[v]=w+dist[u]; 61 now.id=v; 62 now.w=dist[v]; 63 q.push(now); 64 } 65 } 66 } 67 } 68 } 69 typec getdist(int id) { 70 return dist[id]; 71 } 72 } gx; 73 int main(){ 74 int n; 75 char tmp[32]; 76 while(~scanf("%d",&n)){ 77 gx.init(); 78 for(int i=2;i<=n;i++){ 79 for(int j=1;j<i;j++){ 80 scanf("%s",tmp); 81 if(tmp[0]==‘x‘){ 82 continue; 83 } 84 int sum=0; 85 for(int k=0;tmp[k];k++){ 86 sum*=10; 87 sum+=tmp[k]-‘0‘; 88 } 89 gx.add(i,j,sum); 90 gx.add(j,i,sum); 91 } 92 } 93 gx.solve(1); 94 int ans=0; 95 for(int i=1;i<=n;i++){ 96 ans=max(ans,gx.getdist(i)); 97 } 98 printf("%d\n",ans); 99 } 100 return 0; 101 }
Tram http://poj.org/problem?id=1847
dij+priority queue o (elogv)
1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #include<stack> 5 #include<algorithm> 6 #define mt(a,b) memset(a,b,sizeof(a)) 7 using namespace std; 8 typedef __int64 LL; 9 const int inf=0x3f3f3f3f; 10 class Dijkstra { ///单源最短路 o(ME*log(MV)) 11 typedef int typec;///边权的类型 12 static const int ME=10010;///边的个数 13 static const int MV=110;///点的个数 14 struct Q { 15 int id; 16 typec w; 17 friend bool operator <(const Q &a,const Q &b) { 18 return a.w>b.w; 19 } 20 } now; 21 priority_queue<Q> q; 22 struct E { 23 int v,next; 24 typec w; 25 } e[ME]; 26 int le,head[MV]; 27 typec dist[MV]; 28 bool used[MV]; 29 public: 30 void init() { 31 le=0; 32 mt(head,-1); 33 } 34 void add(int u,int v,typec w) { 35 e[le].v=v; 36 e[le].w=w; 37 e[le].next=head[u]; 38 head[u]=le++; 39 } 40 void solve(int s) { 41 for(int i=0; i<MV; i++) { 42 used[i]=true; 43 dist[i]=inf; 44 } 45 dist[s]=0; 46 now.id=s; 47 now.w=0; 48 while(!q.empty()) q.pop(); 49 q.push(now); 50 while(!q.empty()) { 51 now=q.top(); 52 q.pop(); 53 int u=now.id; 54 if(used[u]) { 55 used[u]=false; 56 for(int i=head[u]; ~i; i=e[i].next) { 57 int v=e[i].v; 58 typec w=e[i].w; 59 if(used[v]&&dist[v]>w+dist[u]) { 60 dist[v]=w+dist[u]; 61 now.id=v; 62 now.w=dist[v]; 63 q.push(now); 64 } 65 } 66 } 67 } 68 } 69 typec getdist(int id) { 70 return dist[id]; 71 } 72 } gx; 73 int main(){ 74 int n,a,b; 75 while(~scanf("%d%d%d",&n,&a,&b)){ 76 gx.init(); 77 for(int i=1;i<=n;i++){ 78 int m,to; 79 scanf("%d",&m); 80 for(int j=0;j<m;j++){ 81 scanf("%d",&to); 82 if(j!=0) gx.add(i,to,1); 83 else gx.add(i,to,0); 84 } 85 } 86 gx.solve(a); 87 int ans=gx.getdist(b); 88 if(ans>1024) ans=-1; 89 printf("%d\n",ans); 90 } 91 return 0; 92 }
Til the Cows Come Home http://poj.org/problem?id=2387
dij+priority queue o (elogv)
1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #include<stack> 5 #include<algorithm> 6 #define mt(a,b) memset(a,b,sizeof(a)) 7 using namespace std; 8 typedef __int64 LL; 9 const int inf=0x3f3f3f3f; 10 class Dijkstra { ///单源最短路 o(ME*log(MV)) 11 typedef int typec;///边权的类型 12 static const int ME=1000010;///边的个数 13 static const int MV=1000010;///点的个数 14 struct Q { 15 int id; 16 typec w; 17 friend bool operator <(const Q &a,const Q &b) { 18 return a.w>b.w; 19 } 20 } now; 21 priority_queue<Q> q; 22 struct E { 23 int v,next; 24 typec w; 25 } e[ME]; 26 int le,head[MV]; 27 typec dist[MV]; 28 bool used[MV]; 29 public: 30 void init() { 31 le=0; 32 mt(head,-1); 33 } 34 void add(int u,int v,typec w) { 35 e[le].v=v; 36 e[le].w=w; 37 e[le].next=head[u]; 38 head[u]=le++; 39 } 40 void solve(int s) { 41 for(int i=0; i<MV; i++) { 42 used[i]=true; 43 dist[i]=inf; 44 } 45 dist[s]=0; 46 now.id=s; 47 now.w=0; 48 while(!q.empty()) q.pop(); 49 q.push(now); 50 while(!q.empty()) { 51 now=q.top(); 52 q.pop(); 53 int u=now.id; 54 if(used[u]) { 55 used[u]=false; 56 for(int i=head[u]; ~i; i=e[i].next) { 57 int v=e[i].v; 58 typec w=e[i].w; 59 if(used[v]&&dist[v]>w+dist[u]) { 60 dist[v]=w+dist[u]; 61 now.id=v; 62 now.w=dist[v]; 63 q.push(now); 64 } 65 } 66 } 67 } 68 } 69 typec getdist(int id) { 70 return dist[id]; 71 } 72 } gx; 73 int main(){ 74 int m,n,u,v,w; 75 while(~scanf("%d%d",&m,&n)){ 76 gx.init(); 77 while(m--){ 78 scanf("%d%d%d",&u,&v,&w); 79 gx.add(u,v,w); 80 gx.add(v,u,w); 81 } 82 gx.solve(1); 83 printf("%d\n",gx.getdist(n)); 84 } 85 return 0; 86 }
Subway http://poj.org/problem?id=2502
dij+priority queue o (elogv)
1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<queue> 5 #include<stack> 6 #include<algorithm> 7 #define mt(a,b) memset(a,b,sizeof(a)) 8 using namespace std; 9 typedef __int64 LL; 10 const int inf=0x3f3f3f3f; 11 const int M=210; 12 class Dijkstra { ///单源最短路 o(ME*log(MV)) 13 typedef double typec;///边权的类型 14 static const int ME=M*M;///边的个数 15 static const int MV=M;///点的个数 16 struct Q { 17 int id; 18 typec w; 19 friend bool operator <(const Q &a,const Q &b) { 20 return a.w>b.w; 21 } 22 } now; 23 priority_queue<Q> q; 24 struct E { 25 int v,next; 26 typec w; 27 } e[ME]; 28 int le,head[MV]; 29 typec dist[MV]; 30 bool used[MV]; 31 public: 32 void init() { 33 le=0; 34 mt(head,-1); 35 } 36 void add(int u,int v,typec w) { 37 e[le].v=v; 38 e[le].w=w; 39 e[le].next=head[u]; 40 head[u]=le++; 41 } 42 void solve(int s) { 43 for(int i=0; i<MV; i++) { 44 used[i]=true; 45 dist[i]=inf; 46 } 47 dist[s]=0; 48 now.id=s; 49 now.w=0; 50 while(!q.empty()) q.pop(); 51 q.push(now); 52 while(!q.empty()) { 53 now=q.top(); 54 q.pop(); 55 int u=now.id; 56 if(used[u]) { 57 used[u]=false; 58 for(int i=head[u]; ~i; i=e[i].next) { 59 int v=e[i].v; 60 typec w=e[i].w; 61 if(used[v]&&dist[v]>w+dist[u]) { 62 dist[v]=w+dist[u]; 63 now.id=v; 64 now.w=dist[v]; 65 q.push(now); 66 } 67 } 68 } 69 } 70 } 71 typec getdist(int id) { 72 return dist[id]; 73 } 74 } gx; 75 struct point{ 76 double x,y; 77 }p[M]; 78 double a[M][M]; 79 int main() { 80 int i,j,c,flag=0; 81 scanf("%lf%lf%lf%lf",&p[0].x,&p[0].y,&p[1].x,&p[1].y); 82 int n=2; 83 while(~scanf("%lf%lf",&p[n].x,&p[n].y)) { 84 if(p[n].x==-1&&p[n].y==-1) { 85 flag=0; 86 continue; 87 } 88 if(flag) { 89 a[n][n-1]=sqrt((p[n].x-p[n-1].x)*(p[n].x-p[n-1].x)+(p[n].y-p[n-1].y)*(p[n].y-p[n-1].y))/40000.0; 90 a[n-1][n]=a[n][n-1]; 91 } 92 n++; 93 flag=1; 94 } 95 for(i=0; i<n; i++) { 96 for(j=0; j<n; j++) { 97 if(i!=j&&a[i][j]==0.0) { 98 a[i][j]=sqrt((p[j].x-p[i].x)*(p[j].x-p[i].x)+(p[j].y-p[i].y)*(p[j].y-p[i].y))/10000.0; 99 } 100 } 101 } 102 gx.init(); 103 for(i=0;i<n;i++){ 104 for(j=0;j<n;j++){ 105 gx.add(i,j,a[i][j]); 106 } 107 } 108 gx.solve(0); 109 printf("%0.0f\n",60.0*gx.getdist(1)); 110 return 0; 111 }
end
时间: 2024-11-02 09:51:47