poj 1797 并查集 / floyd(超时)

来源:互联网 发布:短信数据恢复软件 编辑:程序博客网 时间:2024/04/30 09:42

Heavy Transportation
Time Limit: 3000MS Memory Limit: 30000KTotal Submissions: 29437 Accepted: 7865

Description

Background
Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place where it is needed on which all streets can carry the weight.
Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know.

Problem
You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo's place) to crossing n (the customer's place). You may assume that there is at least one path. All streets can be travelled in both directions.

Input

The first line contains the number of scenarios (city plans). For each city the number n of street crossings (1 <= n <= 1000) and number m of streets are given on the first line. The following m lines contain triples of integers specifying start and end crossing of the street and the maximum allowed weight, which is positive and not larger than 1000000. There will be at most one street between each pair of crossings.

Output

The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the maximum allowed weight that Hugo can transport to the customer. Terminate the output for the scenario with a blank line.

Sample Input

13 31 2 31 3 42 3 5

Sample Output

Scenario #1:4



题意:

给出n个城市,m条路,给出相应的路可以运送的重量

从 1 ~ n 这条路上,求可以运的最大的重量



并查集


#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int n,m;int father[100010];struct node{    int s,e;    int weight;}a[100010];int cmp(const void *x,const void *y){    node * A=(node *)x;    node * B=(node *)y;    return B->weight - A->weight;}int find_it(int x){    int tempx=x,t;    while(tempx!=father[tempx])        tempx=father[tempx];    while(x!=father[x])    {        t=father[x];        father[x]=tempx;        x=t;    }    return tempx;}void unite(int a,int b){    int tx=find_it(a);    int ty=find_it(b);    if(tx!=ty)        father[ty]=tx;}int main(){    int t;    int cases=1;    //freopen("in.txt","r",stdin);    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        for(int i=0;i<=n;i++)            father[i]=i;        int ans;        for(int i=0;i<m;i++)            scanf("%d%d%d",&a[i].s,&a[i].e,&a[i].weight);        qsort(a,m,sizeof(a[0]),cmp);        for(int i=0;i<m;i++){            unite(a[i].s,a[i].e);            if(find_it(n)==find_it(1)){//不能find(n) == 1                ans=a[i].weight;//因为s和e的大小不确定                break;            }        }        printf("Scenario #%d:\n%d\n\n",cases++,ans);    }    return 0;}





floyd

超时


但是这里也可以学到一点:

注意以下数据的处理

1   5   5

1   2   3

2   5   9



#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define INF 0x3f3f3f3fint map[1010][1010];int main(){    int t;    freopen("in.txt","r",stdin);    scanf("%d",&t);    int cases=1;    while(t--)    {        int n,m;        scanf("%d%d",&n,&m);        for(int i=1;i<=n;i++){            for(int j=1;j<=n;j++)                map[i][j]=INF;            map[i][i]=0;        }        int a,b,c;        for(int i=1;i<=m;i++){            scanf("%d%d%d",&a,&b,&c);            map[a][b]=map[b][a]=c;//单向双向都一样        }        for(int k=1;k<=n;k++)            for(int i=1;i<=n;i++)                for(int j=1;j<=n;j++){                    int temp=min(map[i][k],map[k][j]);                    if(map[1][n]<temp)//注意                        map[i][j]=min(temp,map[i][j]);                }        printf("Scenario #%d:\n",cases++);        printf("%d\n",map[1][n]);    }    return 0;}


0 0
原创粉丝点击