HDU -- 1, 4370

Source: Internet
Author: User

1001. http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4370

A topic similar to God, which is modeled using graph theory ~ N has n points in total.

X12 + x13 +... x1n = 1
2. x1n + x2n +... xN-1n = 1

It is understood that the outbound degree of point 1 is 1, and the inbound degree of point N is 1;

The matrix is interpreted as the weight on the edge, so it can be understood:

1. The shortest path from 1 to n

2.1 and N respectively.

Mark wrote a dijikstra and orz with priority queue optimization, although I forgot to consider the self-ring.

#include<iostream>#include<cstdio>#include<queue>using namespace std;#define MAXN 330#define INF 100000000int n;int dis[MAXN];int g[MAXN][MAXN];bool vis[MAXN];priority_queue < pair<int,int> > pq;int dijkstra(){int i,S=0,T=n-1;vis[0]=dis[0]=0;pq.push(make_pair(0,S));for(i=1;i<n;i++) dis[i]=INF,vis[i]=false;while(!pq.empty()){int u=pq.top().second;pq.pop();if(vis[u]) continue;vis[u]=true;for(i=0;i<n;i++){if(dis[i]>dis[u]+g[u][i]){dis[i]=dis[u]+g[u][i];pq.push(make_pair(-dis[i],i));}}}return dis[T];}int main(){//freopen("data.in","r",stdin);//freopen("data.out","w",stdout);while(~scanf("%d",&n)){int i,j;for(i=0;i<n;i++)for(j=0;j<n;j++) scanf("%d",&g[i][j]);printf("%d\n",dijkstra());}return 0;}

Self-written dijisktra: a lot of frustration

# Include <cstdio> # include <iostream> using namespace STD; // complexity: O (N ^ 2) const int maxn = 330; const int INF = (1 <30); int mat [maxn] [maxn]; // s indicates the start point, D indicates the end point, and N indicates the matrix size, cannot process negative int Dijkstra (int s, int D, int N) {bool vis [maxn]; int dis [maxn], K = inf, one =-1; memset (VIS, false, sizeof (VIS); fill (DIS, DIS + N, INF); vis [s] = true; DIS [s] = 0; for (INT I = 0; I <n; I ++) dis [I] = mat [s] [I]; while (one! = D) {k = inf; For (INT I = 0; I <n; I ++) if (DIS [I] <K &&! Vis [I]) {one = I, K = dis [I];} vis [one] = true; // cout <"here" <one <Endl; for (INT I = 0; I <n; I ++) if (DIS [I]> dis [one] + mat [one] [I]) dis [I] = dis [one] + mat [one] [I];} return dis [d];} int main () {int N; while (~ Scanf ("% d", & N) {for (INT I = 0; I <n; I ++) for (Int J = 0; j <N; j ++) scanf ("% d", & mat [I] [J]); For (INT I = 0; I <n; I ++) mat [I] [I] = 0; printf ("% d \ n", Dijkstra (0, n-1, n);} return 0 ;}

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.