neuoj 1128 Choose a Way

最短路的变形,,,,应该最好用dijstra算法的,,,,我任性的用了Floyd,,,还是过了。。。。时间比较长(不过好写啊!!!!)

核心变形是这步: dis[i][j] = min(dis[i][j], max(dis[i][k], dis[k][j]));

#include<stdio.h>

#include<string.h>

#include <algorithm>

#include <bits/stdc++.h>

using namespace std;

int dis[1001][1001];

int main()

{

int cas;

int m,n;

scanf("%d",&cas);

int t=1;

while(cas--)

{

scanf("%d%d",&n,&m);

int i,j,k;

for(i=0;i<=n;i++)

{

for(j=0;j<=n;j++)

{

if(i==j) dis[i][j]=0;

else dis[i][j]=999999;

}

}

int u,v,w;

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

{

scanf("%d%d%d",&u,&v,&w);

if(dis[u][v]!=999999)

{

if(dis[u][v]>w)

{

dis[u][v]=w;

dis[v][u]=w;

}

}

else

{

dis[u][v]=w;

dis[v][u]=w;

}

}

for ( k=0; k<n;++k)

for (i=0;i<n; ++i )

for ( j = 0; j < n; ++j )

{

dis[i][j] = min(dis[i][j], max(dis[i][k], dis[k][j]));

}

int q;

scanf("%d",&q);

printf("Case %d:\n",t++);

for(i=0;i<n;i++)

{

if(dis[i][q]==999999) puts("No way");

else  printf("%d\n",dis[i][q]);

}

}

return 0;

}



版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-13 07:59:16

neuoj 1128 Choose a Way的相关文章

ACM--模拟--Inna and Choose Options--水

题目地址:传送门 H - Inna and Choose Options Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description There always is something to choose from! And now, instead of "Noughts and Crosses", Inna choose a v

1128: 零起点学算法35——再求多项式(含浮点)

1128: 零起点学算法35--再求多项式(含浮点) Time Limit: 1 Sec  Memory Limit: 64 MB   64bit IO Format: %lldSubmitted: 2141  Accepted: 1002[Submit][Status][Web Board] Description 输入一个整数n,计算 1+1/(1-3)+1/(1-3+5)+...+1/(1-3+5-...+2n-1)的值 Input 输入一个整数n(多组数据) Output 出1+1/(1

jstl catch if choose标签

catch标签: catch标签用来处理异常 属性: * var :用来出现异常保存到的变量. 代码: <c:catch var="e"> <% int i = 10 / 0; %> </c:catch> ${ e.message } if标签 if标签用来做条件选择 属性: * test :条件 * var :将条件的运算值存入到一个变量 * scope :变量存的四个范围 代码: <c:set var="i" value

自定义标签 (choose)

因为有3个标签,所以写3个标签处理器类 1.ChooseTag public class ChooseTag extends SimpleTagSupport { public Boolean tag = true; public Boolean getTag() { return tag; } public void setTag(Boolean tag) { this.tag = tag; } @Override public void doTag() throws JspException

hdu 2680 Choose the best route 大年三十的首A 赤裸裸的Dijkstra 做这题需要一个小技巧

Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8063    Accepted Submission(s): 2655 Problem Description One day , Kiki wants to visit one of her friends. As she is liable

UVA 10375 Choose and divide(数论)

The binomial coefficient C(m,n) is defined as m! C(m,n) = -------- n!(m-n)! Given four natural numbers p, q, r, and s, compute the the result of dividing C(p,q) by C(r,s). The Input Input consists of a sequence of lines. Each line contains four non-n

HDU2680 Choose the best route 【Dijkstra】

Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7061    Accepted Submission(s): 2300 Problem Description One day , Kiki wants to visit one of her friends. As she is liable

51nod 1128 二分

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1128 1128 正整数分组 V2 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 收藏 关注 给出一个长度为N的正整数数组,不改变数组元素的顺序,将这N个数分为K组.各组中元素的和分别为S1,S2....Sk.如何分组,使得S1至Sk中的最大值最小? 例如:1 2 3 4 5 6分为3组,{1 2 3} {4 5} {6},元素和为6

CodeForces 400A Inna and Choose Options

Inna and Choose Options Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 400A64-bit integer IO format: %I64d      Java class name: (Any) There always is something to choose from! And now, instead of "N