一样的代码,使用函数就1164,全部写进main函数里就160,OMG!。。。。。
//2617181 2011-08-06 10:18:13 Accepted 1298 C++ 0 1164 ylwh!//人家用的内存是我的零头啊,有木有~~!有木有~!!
//2617178 2011-08-06 10:16:59 Presentation Error 1298 C++ 0 1164 ylwh!
//2617097 2011-08-06 09:09:56 Wrong Answer 1298 C++ 0 1164 ylwh!
//2617092 2011-08-06 09:06:27 Wrong Answer 1298 C++ 0 1164 ylwh!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
using namespace std;
int n, m, map[501][501], d[501];
void input()
{
int i, a, b, l;
for(i=1; i<=n; i++)
d[i] = 200000000;
for(i=0; i<m; i++)
{
scanf("%d%d%d", &a, &b, &l);
map[a][b] = map[b][a] = l;
}
}
void dijkstra()
{
int i, j, k, now, maxs, x, y;
float time, temp;
bool flag[501];
memset(flag, true, sizeof(flag));
now = 1;
d[1] = 0;
flag[1] = false;
for(i=1; i<=n; i++){
maxs = 200000000;
for(j=1; j<=n; j++)
if(map[now][j] && d[j] > d[now] + map[now][j])
d[j] = d[now] + map[now][j];
for(j=1; j<=n; j++)
if(flag[j] && maxs > d[j])
{
maxs = d[j];
now = j;
}
flag[now] = false;
}//用dijkstra算出最后一个key开始倒下时的时间
maxs = 0;
x = 1;
for(i=1; i<=n; i++)
if(d[i] > maxs)
maxs = d[i];
time = 0;
flag[1] = true;
for(i=1; i<=n; i++)
if(maxs == d[i])//在最大点处查找
for(j=1; j<=n; j++)
if(map[i][j])
{
temp = (map[i][j] + d[j] - d[i]) * 1.0 / 2 + d[i];//~~
if(temp > time)
{
time = temp;
if(time == d[i])
{
flag[1] = 1;
x = i;
}
if(time > d[i])
{
flag[1] = 0;
x = i;
y = j;
}
}
}
if( flag[1] )
printf("The last domino falls after %.1f seconds, at key domino %d.\n", time, x);
else
printf("The last domino falls after %.1f seconds, between key dominoes %d and %d.\n", time, x>y?y:x, x>y?x:y);//如果是这种情况的话,先输出小的~~
printf("\n");
}
int main(void)
{
int t=0;
while(scanf("%d%d", &n, &m), n)
{
memset(map, 0, sizeof(map));
input();
t++;
printf("System #%d\n", t);
dijkstra();
}
return 0;
}
//2011-08-06 10:56:13 C 0 160 ylwh!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
int t=0, a, b, l, n, m, map[501][501], d[501], i, j, k, now, maxs, x, y;
float time, temp;
int flag[501];
while(scanf("%d%d", &n, &m), n)
{
for(a=1; a<=n; a++)
for(b=1; b<=n; b++)
map[a][b] = 0;
for(i=1; i<=n; i++)
d[i] = 200000000;
for(i=0; i<m; i++)
{
scanf("%d%d%d", &a, &b, &l);
map[a][b] = map[b][a] = l;
}
t++;
printf("System #%d\n", t);
memset(flag, 0, sizeof(flag));
now = 1;
d[1] = 0;
flag[1] = 1;
for(i=1; i<=n; i++)
{
maxs = 200000000;
for(j=1; j<=n; j++)
if(map[now][j] && d[j] > d[now] + map[now][j])
d[j] = d[now] + map[now][j];
for(j=1; j<=n; j++)
if( !flag[j] && maxs > d[j])
{
maxs = d[j];
now = j;
}
flag[now] = 1;
}
maxs = 0;
x = 1;
for(i=1; i<=n; i++)
if(d[i] > maxs)
maxs = d[i];
time = 0;
flag[1] = 1;
for(i=1; i<=n; i++)
if(maxs == d[i])
for(j=1; j<=n; j++)
if(map[i][j])
{
temp = (map[i][j] + d[j] - d[i]) * 1.0 / 2 + d[i];
if(temp > time)
{
time = temp;
if(time == d[i])
{
flag[1] = 1;
x = i;
}
if(time > d[i])
{
flag[1] = 0;
x = i;
y = j;
}
}
}
if( flag[1] )
printf("The last domino falls after %.1f seconds, at key domino %d.\n", time, x);
else
printf("The last domino falls after %.1f seconds, between key dominoes %d and %d.\n", time, x>y?y:x, x>y?x:y);
printf("\n");
}
return 0;
}