POJ 3411-paid Roads (state compression +dijkstra algorithm +floyd-warshall algorithm)

Source: Internet
Author: User

The main idea: to give a map, to find the shortest point of 1 to N, the difference is that for each edge, in addition to the source point and cost, there is an additional point C, if you go to this edge before the C point, the cost will be reduced to another value p. If the shortest circuit does not exist, the output impossible.


The Floyd-warshall algorithm is used to determine the connectivity, and the additional C and P are ignored at this time.

Then use the Dijkstra algorithm, with D[i][s] to indicate at point I and through the S set of points of the shortest, will each d[i][s] as a point, with the Dijkstra algorithm calculation.


#include <stdio.h> #include <stdlib.h> #include <vector> #include <queue>using namespace std; const int inf= (1<<29); struct edge{int from;int to;int sit;int dis1;int dis2;}; struct Heapnode{int di;int num1;int num2;bool operator< (const Heapnode j) Const{return di>j.di;}}; Edge E[15];int d[15][1100];int con[15][15];int use[15][1100];int m,n;void Dijkstra (int s); void Floyd_warshall (void); int main (void) {int i,j,u,p,ans;while (scanf ("%d%d", &n,&m) ==2) {for (i=1;i<=n;i++) {for (j=1;j<=n;j++) { con[i][j]= (i==j)? 1:0;}} for (i=1;i<=m;i++) {scanf ("%d%d%d%d%d", &e[i].from,&e[i].to,&e[i].sit,&e[i].dis2,&e[i]. DIS1); con[e[i].from][e[i].to]=1;} Floyd_warshall (); if (con[1][n]==0) {printf ("impossible\n");} Else{dijkstra (1); ans=inf;u= (1<< (n-1));p = (1<<n); for (j=0;j<p;j++) {if (D[n][j|u|1]<ans) {Ans=d[n] [J|u|1];}} printf ("%d\n", ans);}} return 0;} void Dijkstra (int s) {int i,j,u,v,p;heapnode h;priority_queue

POJ 3411-paid Roads (state compression +dijkstra algorithm +floyd-warshall algorithm)

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.