图论入门题。第一次写的时候用的是比较麻烦的模板,第二次写的时候模板精简多啦。还得要多多努力~
较麻烦的模板,CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
const int INF = 0x0fffffff;
const int SIZE = 110;
int graph[SIZE][SIZE];
int vis[SIZE], dis[SIZE];
int n, m;
void Dijkstra(int src)
{
int i, j;
int min, v;
for(i = 1; i <= n; i++) dis[i] = graph[src][i];
dis[src] = 0;
vis[src] = 1;
for(i = 1; i < n; i++)
{
min = INF;
for(j = 1; j <= n; j++)
{
if(!vis[j] && dis[j] < min)
{
min = dis[j];
v = j;
}
}
vis[v] = 1;
for(j = 1; j <= n; j++)
{
if(!vis[j] && (dis[j] > min+graph[v][j]))
{
dis[j] = min + graph[v][j];
} //类似Prim, dis[j] <?= graph[v][j];
}
}
return ;
}
void init()
{
memset(vis, 0, sizeof(vis));
memset(dis, 0, sizeof(dis));
memset(graph, 0, sizeof(graph));
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
graph[i][j] = graph[j][i] = INF;
}
}
return ;
}
int main()
{
int u, v, w1;
while(~scanf("%d%d", &n, &m))
{
if(n == 0 && m == 0) break;
init();
for(int i = 0 ; i < m; i++)
{
scanf("%d%d%d", &u, &v, &w1);
graph[u][v] = graph[v][u] = w1;
}
Dijkstra(1);
printf("%d\n", dis[n]);
}
#include <stdlib.h>
#include <string.h>
using namespace std;
const int INF = 0x0fffffff;
const int SIZE = 110;
int graph[SIZE][SIZE];
int vis[SIZE], dis[SIZE];
int n, m;
void Dijkstra(int src)
{
int i, j;
int min, v;
for(i = 1; i <= n; i++) dis[i] = graph[src][i];
dis[src] = 0;
vis[src] = 1;
for(i = 1; i < n; i++)
{
min = INF;
for(j = 1; j <= n; j++)
{
if(!vis[j] && dis[j] < min)
{
min = dis[j];
v = j;
}
}
vis[v] = 1;
for(j = 1; j <= n; j++)
{
if(!vis[j] && (dis[j] > min+graph[v][j]))
{
dis[j] = min + graph[v][j];
} //类似Prim, dis[j] <?= graph[v][j];
}
}
return ;
}
void init()
{
memset(vis, 0, sizeof(vis));
memset(dis, 0, sizeof(dis));
memset(graph, 0, sizeof(graph));
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
graph[i][j] = graph[j][i] = INF;
}
}
return ;
}
int main()
{
int u, v, w1;
while(~scanf("%d%d", &n, &m))
{
if(n == 0 && m == 0) break;
init();
for(int i = 0 ; i < m; i++)
{
scanf("%d%d%d", &u, &v, &w1);
graph[u][v] = graph[v][u] = w1;
}
Dijkstra(1);
printf("%d\n", dis[n]);
}
}
精简的模板,CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
const int INF = 0x0fffffff;
const int SIZE = 101;
int w[SIZE][SIZE];
int d[SIZE], v[SIZE];
int N, M;
void Dijkstra(int src)
{
int i, j;
for(i = 1; i <= N; i++) d[i] = (i == src ? 0 : INF);
for(i = 1; i < N; i++)
{
int x, m = INF;
for(int y = 1; y <= N; y++) if(!v[y] && d[y] <= m) m = d[x=y]; //min
v[x] = 1;
for(int y = 1; y <= N; y++) d[y] <?= d[x] + w[x][y]; //松弛
} //类似Prim, d[y] <?= w[x][y];
return ;
} //Dijkstra
void init()
{
memset(w, 0, sizeof(w));
memset(d, 0, sizeof(d));
memset(v, 0, sizeof(v));
for(int i = 1; i <= N; i++)
{
for(int j = 1; j <= N; j++)
{
w[i][j] = INF;
}
}
return ;
}
int main()
{
while(~scanf("%d", &N), N)
{
init();
for(scanf("%d", &M); M > 0; M--)
{
int u, v, w1;
scanf("%d%d%d", &u, &v, &w1);
w[u][v] = w[v][u] = w1;
}
Dijkstra(1);
printf("%d\n", d[N]);
}
#include <stdlib.h>
#include <string.h>
using namespace std;
const int INF = 0x0fffffff;
const int SIZE = 101;
int w[SIZE][SIZE];
int d[SIZE], v[SIZE];
int N, M;
void Dijkstra(int src)
{
int i, j;
for(i = 1; i <= N; i++) d[i] = (i == src ? 0 : INF);
for(i = 1; i < N; i++)
{
int x, m = INF;
for(int y = 1; y <= N; y++) if(!v[y] && d[y] <= m) m = d[x=y]; //min
v[x] = 1;
for(int y = 1; y <= N; y++) d[y] <?= d[x] + w[x][y]; //松弛
} //类似Prim, d[y] <?= w[x][y];
return ;
} //Dijkstra
void init()
{
memset(w, 0, sizeof(w));
memset(d, 0, sizeof(d));
memset(v, 0, sizeof(v));
for(int i = 1; i <= N; i++)
{
for(int j = 1; j <= N; j++)
{
w[i][j] = INF;
}
}
return ;
}
int main()
{
while(~scanf("%d", &N), N)
{
init();
for(scanf("%d", &M); M > 0; M--)
{
int u, v, w1;
scanf("%d%d%d", &u, &v, &w1);
w[u][v] = w[v][u] = w1;
}
Dijkstra(1);
printf("%d\n", d[N]);
}
}