| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:照着《挑战程序设计竞赛》敲下来的代码一直不AC的原因(看错男女)In Reply To:照着《挑战程序设计竞赛》敲下来的代码一直不AC的原因(看错男女) Posted by:liu906326315 at 2017-12-25 20:13:04 > 我看书上的代码,觉得有一行有错误,
> 注意这一行代码:
> //这里为什么要加上女生的人数而不是男生的人数?我认为应该加男生的人数
> es[j].v = v+N;
> 所以我就没这样写,我写的是es[j].v = v+M;在codeblocks运行的好好的,来了poj一直报错,debug两个多小时,终于发现,书里的题目和poj上的题目,x,y表示的正好相反啊啊啊啊啊啊啊!!!!!!!!!
> 书里说,(x,y,d)表示的是第x号男兵和第y号女兵的亲密度是d
> poj的原题说的是第y号男兵和第x号女兵的亲密度是d!!!
>
> 好了 容我歇会儿
> ///////////////////////////分割线////////////////////////////
> 附上AC的源码
> #include <cstdio>
> #include <algorithm>
> #define MAX 10000
> using namespace std;
>
> int par[MAX*2];
> int height[MAX*2];
> int N,M,R;
>
>
> void init_union_find(int v)
> {
> for(int i=0;i<v;i++)
> {
> par[i]=i;
> }
> fill(height,height+v,0);
> }
> int find_root(int x)
> {
> if(par[x]==x)
> {
> return x;
> }
> return find_root(par[x]);
> }
> void unite(int x,int y)
> {
> x = find_root(x);
> y = find_root(y);
> if(x==y)
> {
> return;
> }
>
> if(height[x]>height[y])
> {
> par[y]=x;
> }
> else
> {
> par[x]=y;
> if(height[x]==height[y])
> {
> height[y]++;
> }
> }
> }
> bool same(int x,int y)
> {
> return find_root(x)==find_root(y);
> }
>
>
> struct edge {
> int u,v,cost;
> };
>
> bool comp(const edge& e1,const edge& e2)
> {
> return e1.cost<e2.cost;
> }
> edge es[MAX*5];
>
>
>
> int kruskal()
> {
> sort(es,es+R,comp);
> init_union_find(N+M);
> int res = 0;
> for(int i=0;i<R;i++)
> {
> edge e = es[i];
> //printf("edge e :%d %d %d",es[i].u,es[i].v,es[i].cost);
> if(!same(e.u,e.v))
> {
> unite(e.u,e.v);
> res += e.cost;
> }
> }
> return res;
> }
>
> int main()
> {
> int loop;
> scanf("%d",&loop);
> int u,v,cost;
> for(int i=0;i<loop;i++)
> {
> scanf("%d%d%d",&N,&M,&R);
> for(int j=0;j<R;j++)
> {
> scanf("%d%d%d",&u,&v,&cost);
> es[j].cost = -cost;
> es[j].u = u;
> //这里为什么要加上女生的人数而不是男生的人数?我认为应该加男生的人数
> es[j].v = v+N;
> }
> printf("%d\n",(N+M)*MAX+kruskal());
> }
> return 0;
> }
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator