I can not find my wrong in simple MST problem
Please give me some I/O
Thanks in advance
Moderator: Board moderators
The government now needs your help to decide on the cheapest way of ensuring that every location has access to an airport. The aim is to make airport access as easy as possible, so if there are several ways of getting the minimal cost, choose the one that has the most airports.
if(G[i].wt < weight)
// blah blah blah ......
#include "stdafx.h"
#include<string>
#include<stdio.h>
#include<vector>
#include<string.h>
#include<utility>
#include<queue>
using namespace std;
vector<int> pset(100000);
int findSet(int i) {
return (pset[i] == i) ? i :
(pset[i] = findSet(pset[i]));
}
void initSet(int _size) {
pset.clear();
pset.resize(_size);
for (int i=0; i<_size;i++)
pset[i]=i;
}
void unionSet(int i, int j)
{
pset[findSet(i)]=findSet(j);
}
bool isSameSet(int i, int j) {
return findSet(i)==findSet(j);
}
typedef pair<int,pair<int,int> > iii;
priority_queue < iii , vector < iii > , greater < iii > > edgeList;
int kruskal(int n) { // n = #vertices, m= #aristas, retorna valor MST.
int cont = 0; double mst_cost = 0; initSet(n);
while(!edgeList.empty() && cont < n-1) {
iii front = edgeList.top(); edgeList.pop();
if ( !isSameSet(front.second.first,front.second.second) ) // nodo a y b
{
cont++;
mst_cost += front.first;
unionSet(front.second.first,front.second.second);
}
}
return mst_cost;
}
int casos,vertices,aristas,aero;
int ori,des,pes;
int main()
{
freopen("in.txt","rt",stdin);
freopen("out.txt","wt",stdout);
scanf("%d",&casos);
for(int c=0;c<casos;c++)
{
while(!edgeList.empty())
edgeList.pop();
scanf("%d %d %d",&vertices,&aristas,&aero);// aero= costo por creaeaeropuerto
for(int a=0;a<aristas;a++)
{
iii coso;
scanf("%d %d %d",&ori,&des,&pes);
if(pes<aero)
{
coso.first=pes;coso.second.first=ori-1;coso.second.second=des-1;//ORIgen DEStino PESo
edgeList.push(coso);
}
}
int rpta=kruskal(vertices);
for(int i=0;i<vertices;i++)
findSet(i);
int buliano=0;
for(int i=0;i<vertices;i++)
for(int j=0;j<i;j++)
{
if(pset[i]==pset[j])
{
buliano++;
break;
}
}
rpta+=(aero*(vertices-buliano));//vertices - buliano = cantidad de aeropuertos
printf("Case #%d: %d %d\n",c+1,rpta,vertices-buliano);
}
return 0;
}
if(i != t - 1)
out.printf("Case #%d: %d %d\n", i + 1, totalCost + (long)used.cardinality() * a, used.cardinality());
else out.printf("Case #%d: %d %d", i + 1, totalCost + (long)used.cardinality() * a, used.cardinality());
//if(i != t - 1)
out.printf("Case #%d: %d %d\n", i + 1, totalCost + (long)used.cardinality() * a, used.cardinality());
//else out.printf("Case #%d: %d %d", i + 1, totalCost + (long)used.cardinality() * a, used.cardinality());
Your program should output exactly T lines, one for each case.
brianfry713 wrote:You should print a newline at the end of the last line on most problems.
brianfry713 wrote:Yes it matters how many newlines you print. Don't count on ever getting PE. On a lot of problems extra or missing whitespace will give you WA.
Users browsing this forum: No registered users and 1 guest