Optimal irrigation of CCF real problem

Source: Internet
Author: User

201412-4 problem Description Ray Ray contracted a lot of wheat fields, in order to irrigate the wheat fields, Ray Ray dug a deep well in the first wheat field, all the wheat fields from this well to water diversion irrigation.   In order to irrigate, Ray Ray need to establish some canals to connect wells and wheat fields, and Ray Ray can also use some of the wheat fields as "transit stations", using canals to connect different fields of wheat, so as long as a wheat field can be irrigated, the wheat fields connected to it can also be irrigated. Now Ray Ray know which fields of wheat can be built between canals and the cost of building each channel (note that not all wheat fields can be established between canals). How much is it necessary to irrigate all the wheat fields to build canals? Input format the first line of input contains two positive integers n, m, respectively, representing the number of slices in the wheat field and the amount of canals that Ray Ray can establish. Wheat fields use 1, 2, 3, ...   Label in turn. Next m line, each line contains three integers ai, bi, CI, which indicates that a channel can be established between the AI wheat field and the second Bi wheat field, the cost of which is CI. The output format outputs a line that contains an integer that represents the minimum cost required to irrigate all wheat fields. Sample input 4 4 1 2 1 2 3 4 2 4 2 3 4 3 Sample Output 6 example shows the establishment of the following three canals: Wheat field 1 with wheat field 2, wheat field 2 with wheat field 4, wheat field 4 with wheat Field 3.         Evaluation use case size and the first 20% evaluation use cases meet: n≤5.   The first 40% evaluation cases meet: N≤20.           The first 60% evaluation cases meet: n≤100. All evaluation cases meet: 1≤n≤1000,1≤m≤100,000,1≤ci≤10,000. Source:

#include <bits/stdc++.h>//This header file contains all header files for C + + (very convenient and time-saving programming)

#include <vector>//without the above file, you need to add the following two header files to not error

#include <iostream>

using namespace Std;

const int maxn=1007;

const int inf=10001;

int n,m; struct Node {

int to,cost;

};

BOOL VIS[MAXN];

int D[MAXN];

Vector<node> G[MAXN];

void Init () {

for (int i=0;i<maxn;i++) {

G[i].clear ();

}

}

The minimum number of generated PRIM,CCF can be called directly

int Prim () {

int ans=0;

for (int i=1;i<maxn;i++) {

D[i]=inf;

vis[i]=0;

}

d[1]=0;

while (1) {

int v=-1;

for (int i=1;i<=n;i++) {

if (!vis[i]&& (v==-1| | D[I]<D[V]) v=i;

}

if (v==-1) break;

Vis[v]=1;

ANS+=D[V];

for (int i=0;i<g[v].size (); i++) {

if (vis[g[v][i].to]) continue;

D[g[v][i].to]=min (D[g[v][i].to],g[v][i].cost);

}

}

return ans;

}

int main () {

int a,b,c; Node N1;

while (cin>>n>>m) {

Init ();

for (int i=0;i<m;i++) {

cin>>a>>b>>c;

N1.to=b;n1.cost=c;

G[a].push_back (N1);

N1.to=a;

G[b].push_back (N1);

}

int Ans=prim ();

cout<<ans<<endl;

Used to detect the neighboring nodes of each node and whether the weights are correct

/*for (int i=1;i<=n;i++) {

for (int j=0;j<g[i].size (); j + +)

cout<<g[i][j].to<< "" <<G[i][j].cost<<endl;

}*/        }

}

If the minimum spanning tree is tested again in CCF, the input method is:

First behavior: Given n points, indicates how many wheat fields are currently

The next n lines indicate:

Whether there is a connection between each wheat field and the corresponding weights

If the input method of the question can be changed to:

4

0 1 0 0

1 0 4 2

0 4 0 3

0 2 3 0

The output is still 6 the code in the main function is modified as follows: int main () {int A;
Node N1;
while (Cin>>n) {
Init ();
for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
{
cin>>a;
if (a!=0)
{
N1.to=j+1;n1.cost=a;
G[i+1].push_back (N1);
n1.to=i+1;
G[j+1].push_back (N1);
}

}
int Ans=prim ();
cout<<ans<<endl;

Used to detect the neighboring nodes of each node and whether the weights are correct

/*for (int i=1;i<=n;i++) {

for (int j=0;j<g[i].size (); j + +)

cout<<g[i][j].to<< "" <<G[i][j].cost<<endl;

}*/        }


}}

Optimal irrigation of CCF real problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.