There are n countries at planet X on which Xiao Ming was born.
Some countries, which have been sharing fine bilateral relations, form a coalition and thus all of their citizens will benefit from a policy for which all the travels between these countries will become totally free.
But it is not easy to travel between countries distributed in different coalitions. If possible, it must cost some money called YZB(yu zhou bi) which is always positive.
Help Xiao Ming determine the minimum cost between countries.
First line of each test case consists two integers n and m. (1<=n<=10^5, 1<=m<=10^5)
Each of the following m lines contains: x y c, and c indicating the YZB traveling from x to y or from y to x. If it equals to zero, that means x and y are in the same coalition. (1<=x, y<=n, 0<=c<=10^9)
You can assume that there are no more than one road between two countries.
Then the next line contains an integer q, the number of queries.(1<=q<=200)
Each of the following q lines contains: x y. (1<=x, y<=n)
It is guaranteed that there are no more 200 coalitions.
Input is terminated by a value of zero (0) for n.
1 2 0
2 4 1
3 5 0
1 4 1
1 6 2
3
4 2
1 3
4 6
0
-1
3
题意:就是有n个城市吧。链接任意两个城市需要代价多少,然后有q个询问,求某两个城市之间最小代价是多少,没有连通则输出-1.
题解:最开始想法是把所有能够优化能够变小的代价更新,后来发现开出的二维数组居然不能保存。100000*100000吧。后来用结构体嵌套,居然出现编译超时。好吧。莫名其妙。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
using namespace std;
bool vis[10010][10010];
int dis[10010][10010];
int main()
{
int n,m,s,i,j,x,y,z;
while (~scanf("%d",&n))
{
if (n==0) break;
scanf("%d",&m);
memset(vis,0,sizeof(vis));
memset(dis,-1,sizeof(dis));
for (i=0;i<m;i++)
{
scanf("%d%d%d",&x,&y,&z);
vis[x][y]=vis[y][x]=1;
dis[x][y]=dis[y][x]=z;
}
while (1)
{
int pos=0;
int flag=0;
for (s=1;s<=n;s++)
{
int max=0x7777777f;
for (i=1;i<=n;i++)
if (vis[s][i] && max>dis[s][i] && dis[s][i]!=-1)
{
max=dis[s][i];
pos=i;
}
if (pos==0) continue;
//vis[s][pos]=0;
for (i=1;i<=n;i++)
if (s!=i && vis[pos][i] && (dis[s][i]>max+dis[pos][i]||dis[s][i]==-1))
{
flag=1;
dis[s][i]=dis[i][s]=max+dis[pos][i];
vis[s][i]=vis[i][s]=1;
}
}
if (flag==0) break;
}
int q;
scanf("%d",&q);
while (q--)
{
scanf("%d%d",&x,&y);
if (vis[x][y]) printf("%d\n",dis[x][y]);
else printf("-1\n");
}
}
return 0;
}
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
using namespace std;
struct node1
{
int go,v;
};
struct node
{
int t;
node1 ff[1000];
}f[100010];
int pd(int pos,int j)
{
int i;
for (i=1;i<=f[pos].t;i++)
if (f[pos].ff[i].go==j)
return i;
return 0;
}
int main()
{
int n,m,s,i,j,x,y,z;
while (~scanf("%d",&n))
{
if (n==0) break;
scanf("%d",&m);
memset(f,0,sizeof(f));
for (i=0;i<m;i++)
{
scanf("%d%d%d",&x,&y,&z);
f[x].t++;f[x].ff[f[x].t].go=y;f[x].ff[f[x].t].v=z;
f[y].t++;f[y].ff[f[y].t].go=x;f[y].ff[f[y].t].v=z;
}
while (1)
{
int pos=0;
int flag=0;
for (s=1;s<=n;s++)
{
int max=0x7777777f;
for (i=1;i<=f[s].t;i++)
if (max>f[s].ff[i].v)
{
max=f[s].ff[i].v;
pos=f[s].ff[i].go;
}
if (pos==0) continue;
//vis[s][pos]=0;
for (i=1;i<=n;i++)
{
if (pos == i || i==s) continue;
int k=pd(pos,i); //后一段第几个 pos k
int k2=pd(s,i); // s到i的第几个 s,k2
if (k2 && k /*vis[pos][i]*/ && (f[s].ff[k2].v>max+f[pos].ff[k].v) || (f[s].ff[k2].v==0 && k))
{
flag=1;
if (f[s].ff[k2].v==0 && k)
{
f[s].t++;
f[s].ff[f[s].t].go=i;
f[s].ff[f[s].t].v=max+f[pos].ff[k].v;
}
else
f[s].ff[k2].v=max+f[pos].ff[k].v;
}
}
}
if (flag==0) break;
}
int q;
scanf("%d",&q);
while (q--)
{
scanf("%d%d",&x,&y);
int k=pd(x,y);
if (k) printf("%d\n",f[x].ff[k].v);
else printf("-1\n");
}
}
return 0;
}
看了网上大神的代码,突然觉得自己还是太年轻了...题目上这么大的坑都没有发现,不超过200个联盟,还开毛线二维数组。什么压缩,并查集,floyd就OK了。果然还是太年轻了。。。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
using namespace std;
#define Max 100005
#define INF 0x3fffffff
int dis[210][210];
int dir[Max],u[Max],v[Max],c[Max],f[Max];
int find(int x)
{
if (f[x]!=x) return f[x]=find(f[x]);
else return x;
}
void unit(int x,int y)
{
int a=find(x);
int b=find(y);
if (a!=b) f[a]=b;
return;
}
void floyd(int n)
{
int i,j,k;
for (k=0;k<n;k++)
for (i=0;i<n;i++)
for (j=0;j<n;j++)
if (dis[i][k]<INF && dis[j][k]<INF)
if (dis[i][j]>dis[i][k]+dis[k][j])
dis[i][j]=dis[i][k]+dis[k][j];
}
int main()
{
int n,m,i,j,x,y,a,b,q;
while (~scanf("%d",&n) && n)
{
scanf("%d",&m);
for (i=0;i<Max;i++)
f[i]=i;
for (i=0;i<205;i++)
for (j=0;j<205;j++)
if (i==j) dis[i][j]=0;
else dis[i][j]=dis[j][i]=INF;
for (i=1;i<=m;i++)
{
scanf("%d%d%d",&u[i],&v[i],&c[i]);
if (c[i]==0) unit(u[i],v[i]);
}
int t=0;
for (i=1;i<=n;i++)
if (f[i]==i)
dir[i]=t++;
for (i=1;i<=n;i++)
if (f[i]!=i)
{
x=find(i);
dir[i]=dir[x];
}
for (i=1;i<=m;i++)
{
x=find(u[i]);
y=find(v[i]);
if (x!=y)
{
int a=dir[x];
int b=dir[y];
if (c[i]<dis[a][b])
dis[a][b]=dis[b][a]=c[i];
}
}
floyd(t);
scanf("%d",&q);
while (q--)
{
scanf("%d%d",&x,&y);
a=find(x);
b=find(y);
if (a==b)
{
printf("0\n");
continue;
}
a=dir[a];
b=dir[b];
if (dis[a][b]==INF) printf("-1\n");
else printf("%d\n",dis[a][b]);
}
}
return 0;
}