Title Link: http://acm.nyist.net/JudgeOnline/problem.php?pid=115
Algorithm Analysis:
Single source Shortest path problem, SPFA or Dijkstra algorithm
#include <iostream> #include <cstdlib> #include <algorithm> #include <cstdio> #include < cstring>using namespace std; #define MAX_V 1005#define INF 1e8int cost[max_v][max_v];int d[max_v];bool Used[max_v]; int n,m,p,q;int army[max_v];void Dijkstra (int s) {for (int i=1;i<=m;i++) D[i]=inf;memset (used,false,sizeof (used));d [S]=0;while (true) {int v=-1;for (int u=1;u<=m;u++) {if (!used[u]&& (v==-1| | D[U]<D[V]) V=u;} if (v==-1) break;used[v]=true;for (int u=1;u<=m;u++) {d[u]=min (D[u],d[v]+cost[v][u]);}}} int main () {int t;int a,b,t,ans;scanf ("%d", &t), while (t--) {scanf ("%d%d%d%d", &n,&m,&p,&q); Be sure to see whether the given vertex in the title is from 0 or 1 for (int i=1;i<=m;i++) for (int j=1;j<=m;j++) cost[i][j]=i==j?0:inf;for (int i=0;i<n;i+ +) scanf ("%d", &army[i]); for (int i=0;i<p;i++) {scanf ("%d%d%d", &a,&b,&t); Cost[a][b]=t;cost[b][a] =t;} Dijkstra (Q); ans=inf;for (int i=0;i<n;i++) ans=min (Ans,d[army[i]]);p rintf ("%d\n", ans);} return 0;}
Nyoj 115 City insurgency