Nyoj 115 City insurgency

Source: Internet
Author: User

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]&LT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.