মঙ্গলবার, ১৪ ফেব্রুয়ারী, ২০১৭

UVA 544 - Heavy Cargo

problem hints:
Solution way:
             Create an arraylist for mapping, so that as you're taking the adjacencies in, you can just use                  arraylist.indexOf(x) to get the appropriate index you'll use for the adjacency matrix. In this                  process, if the arraylist does not contain the city already, just add it. The result is, you can just              do:

            int input = scan.nextInt();
            dist[arraylist.indexOf(a)][arraylist.indexOf(b)] = input;
            dist[arraylist.indexOf(b)][arraylist.indexOf(a)] = input;

            Then, run floyd warshall on the adjacency matrix with one modification - instead of taking the             min(dist[i][j], dist[i][k]+dist[k][j]), you have to take max(min(dist[i][k], dist[j][k]), dist[i][j])
Problem code:
#include<bits/stdc++.h>
using namespace std;
long i,j,k;
long vec[500][500];
long check(long n1)
{
    for(k=0;k<=n1;k++)
    {
        for(i=0;i<=n1;i++)
        {
            for(j=0;j<=n1;j++)
            {
                vec[i][j]=max(min(vec[i][k],vec[k][j]),vec[i][j]);
            }
        }
    }
}
main()
{
    long long n,m,cs=1;
    while(cin>>n>>m)
    {
        if(n==0&&m==0)
            break;
        for(i=0;i<=n;i++)
        {
            for(j=0;j<=n;j++)
            {
                if(i==j)
                    vec[i][j]==0;
                else
                    vec[i][j]=-1;
            }
        }
        string s,s1;
        long wt,cnt=1;
        map<string,int>mp;
        for(i=1;i<=m;i++)
        {
            cin>>s>>s1>>wt;
            if(mp[s]==0)
            {
                mp[s]=cnt++;
            }
            if(mp[s1]==0)
            {
                mp[s1]=cnt++;
            }
            vec[mp[s]][mp[s1]]=wt;
            vec[mp[s1]][mp[s]]=wt;
        }
        check(n);
        cin>>s>>s1;
        long ans=vec[mp[s]][mp[s1]];
        printf("Scenario #%ld\n",cs++);
        cout<<ans<<" tons"<<endl<<endl;
    }
}

কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন

Factorization with prime Sieve

vector <int> prime; char sieve[1000009]; int N=1000009; void primeSieve ( ) { sieve[0] = sieve[1] = 1; prime.push_back(2); ...