Cod sursa(job #1923322)

Utilizator Kln1000Ciobanu Bogdan Kln1000 Data 10 martie 2017 22:35:05
Problema Ubuntzei Scor 25
Compilator cpp Status done
Runda Arhiva de probleme Marime 6.33 kb
#include <iostream>
#include <fstream>
#include <bitset>
#include <vector>
#include <queue>
#include <cstring>

#define INF 0x7FFFFFF

using namespace std;

/**

                                          `-.`'.-'
                                       `-.        .-'.
                                    `-.    -./\.-    .-'
                                        -.  /_|\  .-
                                    `-.   `/____\'   .-'.
                                 `-.    -./.-""-.\.-      '
                                    `-.  /< (()) >\  .-'
                                  -   .`/__`-..-'__\'   .-
                                ,...`-./___|____|___\.-'.,.
                                   ,-'   ,` . . ',   `-,
                                ,-'   ________________  `-,
                                   ,'/____|_____|_____\
                                  / /__|_____|_____|___\
                                 / /|_____|_____|_____|_\
                                ' /____|_____|_____|_____\
                              .' /__|_____|_____|_____|___\
                             ,' /|_____|_____|_____|_____|_\
,,---''--...___...--'''--.. /../____|_____|_____|_____|_____\ ..--```--...___...--``---,,
                           '../__|_____|_____|_____|_____|___\
      \    )              '.:/|_____|_____|_____|_____|_____|_\               (    /
      )\  / )           ,':./____|_____|_____|_____|_____|_____\             ( \  /(
     / / ( (           /:../__|_____|_____|_____|_____|_____|___\             ) ) \ \
    | |   \ \         /.../|_____|_____|_____|_____|_____|_____|_\           / /   | |
 .-.\ \    \ \       '..:/____|_____|_____|_____|_____|_____|_____\         / /    / /.-.
(=  )\ `._.' |       \:./ _  _ ___  ____  ____ _    _ _ _ _ _  _ __\        | `._.' /(  =)
 \ (_)       )        \/                                            \       (       (_) /
  \    `----'          """"""""""""""""""""""""""""""""""""""""""""""        `----'    /
   \   ____\__                                                              __/____   /
    \ (=\     \                                                            /     /-) /
     \_)_\     \                                                         /     /_(_/
          \     \                                                        /     /
           )     )  _                                                _  (     (
          (     (,-' `-..__                                    __..-' `-,)     )
           \_.-''          ``-..____                  ____..-''          ``-._/
            `-._                    ``--...____...--''                    _.-'
                `-.._                                                _..-'
                     `-..__       FORTIS FORTUNA ADIUVAT       __..-'
                           ``-..____                  ____..-''
                                    ``--...____...--''

*/

class parser{
    public:
        parser() {}
        parser(const char *file_name){
            input_file.open(file_name,ios::in | ios::binary);
            input_file.sync_with_stdio(false);
            index&=0;
            input_file.read(buffer,SIZE);}
        inline parser &operator >>(int &n){
            for (;buffer[index]<'0' or buffer[index]>'9';inc());
            n&=0;
            //sign&=0;
            //sign|=(buffer[index-1]=='-');
            for (;'0'<=buffer[index] and buffer[index]<='9';inc())
                n=(n<<1)+(n<<3)+buffer[index]-'0';
            //n^=((n^-n)&-sign);
            return *this;}
        ~parser(){
            input_file.close();}
    private:
        fstream input_file;
        static const int SIZE=0x400000; ///4MB
        char buffer[SIZE];
        int index/*,sign*/;
        inline void inc(){
            if(++index==SIZE)
                index=0,input_file.read(buffer,SIZE);}
};

class writer{
    public:
        writer() {};
        writer(const char *file_name){
            output_file.open(file_name,ios::out | ios::binary);
            output_file.sync_with_stdio(false);
            index&=0;}
        inline writer &operator <<(int target){
            aux&=0;
            n=target;
            if (!n)
                nr[aux++]='0';
            for (;n;n/=10)
                nr[aux++]=n%10+'0';
            for(;aux;inc())
                buffer[index]=nr[--aux];
            return *this;}
        inline writer &operator <<(const char *target){
            aux&=0;
            while (target[aux])
                buffer[index]=target[aux++],inc();
            return *this;}
        ~writer(){
            output_file.write(buffer,index);output_file.close();}
    private:
        fstream output_file;
        static const int SIZE=0x200000; ///2MB
        int index,aux,n;
        char buffer[SIZE],nr[24];
        inline void inc(){
            if(++index==SIZE)
                index&=0,output_file.write(buffer,SIZE);}
};

parser f ("ubuntzei.in");
writer t ("ubuntzei.out");

struct edge{
    int to,cost;
};

vector <edge> v[2010];
int target[2010],best,best_node,n,m,k;

void bellman_ford(int nod){
    vector <int> dist(2010,INF);
    bitset <2010> in_queue;
    queue <int> q;
    q.push(nod);
    in_queue[nod]=true;
    dist[nod]=0;
    while (!q.empty()){
        int current=q.front();
        in_queue[current]=false;
        if (target[current] and dist[current]<best)
            if (current!=n or (current==n and !k))
                best=dist[current],
                best_node=current;
        q.pop();
        for (auto i:v[current])
            if (dist[i.to]>dist[current]+i.cost){
                dist[i.to]=dist[current]+i.cost;
                if (!in_queue[i.to])
                    q.push(i.to),
                    in_queue[i.to]=true;
            }
    }
}

int main()
{
    f>>n>>m>>k;
    for (int aux,i=0;i<k;++i)
        f>>aux,
        target[aux]=true;
    target[n]=true;
    for (int x,y,c,i=0;i<m;++i)
        f>>x>>y>>c,
        v[x].push_back({y,c}),
        v[y].push_back({x,c});
    int current=1,drum=0;
    while (k){
        best=INF;
        bellman_ford(current);
        drum+=best;
        current=best_node;
        target[best_node]=false;
        --k;
    }
    best=INF;
    bellman_ford(current);
    drum+=best;
    t<<drum;
    return 0;
}