Pagini recente » Cod sursa (job #2783480) | Cod sursa (job #1366772) | Cod sursa (job #40737) | Cod sursa (job #1281638) | Cod sursa (job #1638837)
#include <iostream>
#include <fstream>
#include <queue>
#include <algorithm>
using namespace std;
int n,m,k;
bool comp(pair<int,int> a, pair<int,int> b)
{
return a.second < b.second;
};
struct node
{
bool *sz;
int counter;
int n;
node(int a = 0)
{
sz = new bool[k];
for(int i =0; i < k; i++)
sz[i]=0;
counter = 0;
n=a;
}
bool getsz()
{
if(counter == k)
return true;
return false;
}
};
int main()
{
cout << "hunibuntu" << endl;
ifstream be("ubuntzei.in");
ofstream ki("ubuntzei.out");
be >> n >> m;
be >> k;
int sz[n];
for(int i = 0; i < n; i++)
sz[i] = 0;
for(int i = 0; i < k; i++)
{
int csz;
be >> csz;
sz[csz-1]=i+1;
}
int a[n][n];
int d[n], volt[n], dbest[n];
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
a[i][j]=0;
d[i] = -1;
volt[i] = 0;
dbest[i]=0;
}
for(int i = 0; i < m; i++)
{
int x, y, c;
be >> x >> y >> c;
a[x-1][y-1]=c;
a[y-1][x-1]=c;
}
d[0]=0;
queue<node> vsor;
vsor.push(node(0));
while(!vsor.empty())
{
node c = vsor.front();
vsor.pop();
volt[c.n] = true;
vector<pair<int,int> > lista;
for(int i = 0; i < n; i++)
{
//cout << a[c][i] << endl;
if(a[c.n][i]>0 && (d[i]==-1 || !volt[i] || ( d[i] > d[c.n]+a[c.n][i] && c.counter >= dbest[i]) || sz[i]>0 || c.counter > dbest[i]))
{
if(c.counter > dbest[i])
dbest[i] = c.counter;
if(i==n-1)
{
if(c.getsz())
{
lista.push_back(make_pair(i,a[c.n][i]));
d[i] = d[c.n]+a[c.n][i];
}
}
else
{
d[i] = d[c.n]+a[c.n][i];
lista.push_back(make_pair(i,a[c.n][i]));
}
// if(i==n-1 && c.getsz())
// {
// while(!vsor.empty())
// vsor.pop();
//
// break;
// }
}
}
sort(lista.begin(),lista.end(),comp);
for(int i = 0; i < lista.size(); i++)
{
node newn = c;
newn.n = lista[i].first;
if(k>0)
if(sz[lista[i].first]>0)
if(!newn.sz[sz[lista[i].first]-1])
{
newn.sz[sz[lista[i].first]-1]=1;
newn.counter++;
if(dbest[newn.n]<newn.counter)
dbest[newn.n]=newn.counter;
}
vsor.push(newn);
}
// for(int i = 0; i < n; i++)
// cout << d[i] << " ";
// cout << endl;
}
ki << d[n-1] << endl;
return 0;
}