Cod sursa(job #1838234)

Utilizator tziplea_stefanTiplea Stefan tziplea_stefan Data 31 decembrie 2016 15:07:51
Problema Sate Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.6 kb
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
#define VAL 30005
#define INF 1000000000

using namespace std;

ifstream fin("sate.in");
ofstream fout("sate.out");

int N, M, x, y, i, j;
int a, b, c;
int cost[VAL];
queue<int> q;
vector<int> v[VAL];
vector<int> p[VAL];
vector< pair<int, int> > G[VAL];
vector< pair<int, int> > :: iterator it;

void bfs()
{
    vector<int> :: iterator it;
    vector<int> :: iterator itcost;
    cost[x]=0;
    q.push(x);
    while (q.empty()==false)
    {
        a=q.front();
        q.pop();
        itcost=p[a].begin();
        for (it=v[a].begin(); it!=v[a].end(); it++)
        {
            if (cost[*it]>cost[a]+(*itcost))
            {
                cost[*it]=cost[a]+(*itcost);
                q.push(*it);
            }
            itcost++;
        }
    }
}

int main()
{
    fin >> N >> M >> x >> y;
    for (i=1; i<=M; i++)
    {
        fin >> a >> b >> c;
        if (a>b)
          swap(a, b);
        G[a].push_back(make_pair(b, c));
    }
    for (i=1; i<=N; i++)
    {
        cost[i]=INF;
        if (G[i].size()!=0)
        {
            sort(G[i].begin(), G[i].end());
            it=G[i].begin();
            a=(*it).first;
            c=(*it).second;
            b=0;
            for (it=G[i].begin(); it!=G[i].end(); it++)
            {
                b++;
            //    if (b!=1)
              //    G[a].push_back(make_pair((*it).first, ((*it).second)-c));
            }
            v[i].push_back(a);
            p[i].push_back(c);
        }
    }
    bfs();
    fout << cost[y] << '\n';
    fin.close();
    fout.close();
    return 0;
}