hdu 1598

来源:互联网 发布:阿里云短信发送api 编辑:程序博客网 时间:2024/05/20 17:23
#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <vector>#include <map>#include <set>#include <string>#include <cstring>#include <list>#include <queue>#include <stack>#include <cmath>using namespace std;#define PF(x) (scanf("%d",&x))#define PT(x,y) (scanf("%d%d",&x,&y))#define PR(x) (printf("%d\n",x))#define PRT(x,y)(printf("%d %d\n",x,y))#define PB(x)(scanf("%I64d",&x))#define PRB(x)(printf("%I64d\n",(x)))typedef __int64 LL;#define N 1005#define M 205#define Mod 1000#define Inf 0x7fffffffstruct P{    int s,t,speed;    void in()    {        PT(s,t);        PF(speed);    }};int ar[M];P edge[N];int n,m;int cmp(P & a,P & b){    return a.speed<b.speed;}int find(int a){    int r = a;    while(ar[r] != r)    {        r = ar[r];    }    return r;}void init(){    while(scanf("%d%d",&n,&m)==2)    {        for(int i=0;i<m;i++)            edge[i].in();        sort(edge,edge+m,cmp);        int q;        PF(q);        for(int i=0;i<q;i++)        {            int s,t;            PT(s,t);            int mins = Inf;            for(int k=0;k<m;k++)            {                for(int j=0;j<=n;j++)                {                    ar[j] = j;                }                for(int j=k;j<m;j++)                {                    int a = find(edge[j].s);                    int b = find(edge[j].t);                    ar[b] = a;                    if(find(t) == find(s))                    {                        if(mins>edge[j].speed - edge[k].speed)                            mins = edge[j].speed - edge[k].speed;                        break;                    }                }                if(mins == Inf) break;            }            if(mins == Inf)            {                puts("-1");            }            else PR(mins);            }    }    return ;}int main(){    init();    return 0;}

原创粉丝点击