【POJ 2485】 Highways

最小生成树模板

Prim

#include

using namespace std;

int mp[501][501];
int dis[501];
bool vis[501];
int n; int Prim()
{
int i,j,w,p,mm = 0;
memset(dis,-1,sizeof(dis));
memset(vis,0,sizeof(vis));
dis[1] = 0;
for(i = 0; i < n; ++i)
{
w = INF;
for(j = 1; j <= n; ++j)
{
if(!vis[j] && dis[j] < w && dis[j] != -1)
{
p = j;
w = dis[j];
}
}
vis[p] = 1;
mm = max(mm,w);
for(j = 1; j <= n; ++j)
{
if(!vis[j] && (dis[j] == -1 || dis[j] > mp[p][j]))
dis[j] = mp[p][j];
}
}
return mm;
} int main()
{
int t,i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i = 1; i <= n; ++i)
{
for(j = 1; j <= n; ++j)
{
scanf("%d",&mp[i][j]);
}
}
printf("%d\n",Prim());
}
return 0;
}

Kruskal

#include

using namespace std;

typedef struct Edge
{
int u,v,w;
bool operator < (const struct Edge a)const
{
return w < a.w;
}
}Edge; Edge eg[255555];
int pre[555];
int tp; void Init(int n)
{
int i;
for(i = 1; i <= n; ++i)
pre[i] = i;
} int Find(int x)
{
if(x != pre[x]) pre[x] = Find(pre[x]);
return pre[x];
} int main()
{
int t,n,i,j,w,k,r,mm,cnt;
scanf("%d",&t);
while(t--)
{
tp = 0;
scanf("%d",&n);
Init(n);
for(i = 1; i <= n; ++i)
{
for(j = 1; j <= n; ++j)
{
scanf("%d",&w);
if(j > i)
{
eg[tp].w = w;
eg[tp].u = i;
eg[tp++].v = j;
}
}
}
sort(eg,eg+tp);
mm = cnt = 0;
for(i = 0; i < tp; ++i)
{
k = Find(eg[i].u);
r = Find(eg[i].v);
if(k != r)
{
pre[k] = r;
mm = max(mm,eg[i].w);
cnt++;
}
if(cnt == n-1) break;
}
printf("%d\n",mm);
}
return 0;
}

【POJ 2485】 Highways的更多相关文章

  1. 【POJ 2485】Highways(Prim最小生成树)

    题目 Prim算法:任选一个点,加入集合,找出和它最近的点,加入集合,然后用加入集合的点去更新其它点的最近距离......这题求最小生成树最大的边,于是每次更新一下最大边. #include < ...

  2. bzoj 2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...

  3. 【链表】BZOJ 2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 382  Solved: 111[Submit][S ...

  4. BZOJ2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 284  Solved: 82[Submit][St ...

  5. BZOJ2293: 【POJ Challenge】吉他英雄

    2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 80  Solved: 59[Submit][Stat ...

  6. BZOJ2287: 【POJ Challenge】消失之物

    2287: [POJ Challenge]消失之物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 254  Solved: 140[Submit][S ...

  7. BZOJ2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 126  Solved: 90[Submit][Sta ...

  8. BZOJ2296: 【POJ Challenge】随机种子

    2296: [POJ Challenge]随机种子 Time Limit: 1 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 114  Solv ...

  9. BZOJ2292: 【POJ Challenge 】永远挑战

    2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 513  Solved: 201[Submit][ ...

随机推荐

  1. [SharePoint][SharePoint Designer 入门经典]Chapter8 XSLT数据试图和表单

    本章概要: 1.不是利用XSLT web部件 2.使用XSLT web部件创建数据试图 3.使用XSLT表单web部件创建自定义表单 4.利用自定义动作执行列表表单

  2. 两个NSMutableDictionary合并成一个NSMutableDictionary

    解决方案: NSMutableDictionary *targetMutableDictionary = [mutableDictionary1 copy]; [targetMutableDictio ...

  3. 算法入门经典第六章 例题6-14 Abbott的复仇(Abbott's Revenge)BFS算法实现

    Sample Input 3 1 N 3 3 1 1 WL NR * 1 2 WLF NR ER * 1 3 NL ER * 2 1 SL WR NF * 2 2 SL WF ELF * 2 3 SF ...

  4. C# 实现透明可移动窗体

    1.设置窗体属性 this.BackColor this.TransparencyKey = this.BackColor; 2.窗体加载图片 this.BackgroundImage = globa ...

  5. SQL Server数据库性能优化

      开篇:    最近遇到了很多性能问题,一直没来的及总结,今天正好周末抽时间总结下: 对于稍微大点的公司或者说用户多一些的公司,说白了就是数据量较大的公司,在查询数据时往往会遇到很多瓶颈.这时就需要 ...

  6. ifame子页实现父页面刷新(或跳转到指定页面)

    <script>parent.location.replace('../D_DailyManager/Add.aspx?id=" + x + "');</scri ...

  7. JavaScript实现乘法表

    JavaScript实现乘法表 <script type="text/javascript">        function c(n,m)        {      ...

  8. Vue.js 2.x Development Build With Hot Reloading For External Server (using Webpack template)

    This article assuming you created your project using webpack template. vue init webpack <PROJECT_ ...

  9. 使用JS&jQuery改善用户体验

    第一章  JavaScript基本语法 一.运算符 运算符就是完成操作的一系列符号,它有七类: 赋值运算符(=,+=,-=,*=,/=,%=,<<=,>>=,|=,&= ...

  10. DataReader相关知识点

    C#中提供的DataReader可以从数据库中每次提取一条数据. 1. 获取数据的方式[1]DataReader 为在线操作数据, DataReader会一直占用SqlConnection连接,在其获 ...