标签:des blog io os ar java for sp strong
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14009 Accepted Submission(s): 4304
加了输入挂,果然快了一倍。然后个人还是不够严谨
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
#define N 1010
int d[N][N],cost[N][N],vis[N],dist[N],cst[N];
int n,m,a,b,dd,p,s,t;
const int inf = 0X700000;
int in() //输入挂
{
int r = 0;
char ch;
ch = getchar();
while(ch < ‘0‘ || ch > ‘9‘) ch = getchar();
while(ch >= ‘0‘ && ch <= ‘9‘)
{
r = r*10 + ch-‘0‘;
ch = getchar();
}
return r;
}
void init()
{
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++)
{
d[i][j] = cost[i][j] = inf;
d[j][i] = cost[j][i] = inf;
}
vis[i] = 0;
dist[i] = cst[i] = inf;
}
}
void input()
{
while(m--)
{
//scanf("%d %d %d %d",&a,&b,&dd, &p);
a = in();
b = in();
dd = in();
p = in();
if(dd < d[a][b]){
d[a][b]= d[b][a] = dd;
cost[a][b] = cost[b][a] = p;
}else if(dd == d[a][b] && p < cost[a][b])
{
cost[a][b] = cost[b][a] = p;
}
//d[a][b] = d[b][a] = min(d[a][b],dd); 这行错误 = =!!果然还是不够严谨
//cost[a][b] = cost[b][a] = min(cost[a][b], p);
}
s = in(); t = in();
}
void dij()
{
for(int i = 1; i <= n; i++){
dist[i] = d[s][i];
cst[i] = cost[s][i];
}
vis[s] = 1;
dist[s] = 0;
cst[s] = 0;
for(int i = 2; i <= n; i++)
{
int mn = inf;
int pos = 1;
for(int j = 1; j <= n; j++)
if(vis[j] == 0 && dist[j] < mn) {pos = j; mn = dist[j];}
vis[pos] = 1;
for(int j = 1; j <= n; j++)
{
if(vis[j] == 0 && d[pos][j] < inf)
{
int newd = dist[pos] + d[pos][j];
int newc = cst[pos] + cost[pos][j];
if(newd < dist[j]) {
dist[j] = newd;
cst[j] = newc;
}else if(newd == dist[j] && newc < cst[j])
cst[j] = newc;
}
}
}
}
int main()
{
while(scanf("%d %d", &n, &m) && (m||n))
{
init();
input();
dij();
printf("%d %d\n", dist[t], cst[t]);
}
return 0;
}
标签:des blog io os ar java for sp strong
原文地址:http://www.cnblogs.com/brokesb/p/4064014.html