ACdream原創群賽(16) F

來源:互聯網
上載者:User

標籤:acdream


MSTTime Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)SubmitStatusProblem Description
Given a connected, undirected graph, a spanning tree of that graph is a subgraph that is a tree and connects all the vertices together.  A single graph can have many different spanning trees. We can also assign a weight to each edge, which is a number representing how unfavorable it is, and use this to assign a weight to a spanning tree by computing the sum of the weights of the edges in that spanning tree. A minimum spanning tree (MST) is then a spanning tree with weight less than or equal to the weight of every other spanning tree.
------ From wikipedia
Now we make the problem more complex. We assign each edge two kinds of weight: length and cost. We call a spanning tree with sum of length less than or equal to others MST. And we want to find a MST who has minimal sum of cost.
Input
There are multiple test cases.
The first line contains two integers N and M indicating the number of vertices and edges in the gragh.
The next M lines, each line contains three integers a, b, l and c indicating there are an edge with l length and c cost between a and b.
1 <= N <= 10,000
1 <= M <= 100,000
1 <= a, b <= N
1 <= l, c <= 10,000
Output
For each test case output two integers indicating the sum of length and cost of corresponding MST.
If you can find the corresponding MST, please output "-1 -1".
Sample Input
4 5
1 2 1 1 
2 3 1 1
3 4 1 1
1 3 1 2
2 4 1 3
Sample Output
3 3



題意比較坑人,可以存在不連通的圖。

這是一個稀疏圖,故用kruskal演算法!!!




AC代碼如下:

#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#define inf 100000000using namespace std; int f[10005]; struct H{    int l,r,d,c;}w[100005]; int find (int a){    return f[a]==a? a : f[a]=find(f[a]);} bool cmp(H a, H b){    return a.d<b.d||(a.d==b.d&&a.c<b.c);//主要注意這個條件的判斷就可以了}int main(){    int n,m;    int i,j;    int a,b,c,d;    int ans;    while(cin>>n>>m)    {        memset(w,0,sizeof w);        for(i=1;i<=n;i++)            f[i]=i;        for(i=1;i<=m;i++)        {            cin>>a>>b>>c>>d;            w[i].l=a;            w[i].r=b;            w[i].d=c;            w[i].c=d;        }        int ans=0,cost=0,bj=0;        sort(w+1,w+m+1,cmp);        for(i=1;i<=m;i++)        {            int xx,yy;            xx=find(w[i].l);            yy=find(w[i].r);            if(xx!=yy)            {                bj++;                ans+=w[i].d;                cost+=w[i].c;                f[xx]=yy;            }        }        if(bj==n-1)        printf("%d %d\n",ans,cost);        else printf("-1 -1\n");    }    return 0;}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.