Contest2037 - CSU Monthly 2013 Oct (problem D :CX and girls)

【题解】:

最短路径问题,保证距离最短的同时,学妹权值最大,哈哈

【code】:

  1 #include<iostream>
  2 #include<queue>
  3 #include<stdio.h>
  4 #include<string.h>
  5 #include<stdlib.h>
  6 
  7 #define N 50005
  8 #define INF 100000000
  9 using namespace std;
 10 
 11 struct Edge
 12 {
 13     int to;
 14     int next;
 15     int w;
 16     int num;
 17 }edge[N<<1];
 18 
 19 struct Nod
 20 {
 21    int u;
 22    int dis;
 23    int num;
 24 }now,temp;
 25 
 26 bool operator< (Nod a,Nod b)
 27 {
 28     if(a.dis!=b.dis)
 29         return a.dis>b.dis;
 30     return a.num<b.num;
 31 }
 32 
 33 int weight[N],head[N],visit[N];
 34 int dis[N];
 35 int ans[N];
 36 
 37 void init(int n)
 38 {
 39     int i;
 40     for(i=0;i<=n;i++)
 41     {
 42         visit[i] = 0;
 43         dis[i] = INF;
 44         head[i] = -1;
 45         ans[i]=-1;
 46     }
 47 }
 48 
 49 void Dijkstra(int s)
 50 {
 51     int i,v;
 52     dis[s] = 0;
 53     ans[s] = weight[s];
 54     priority_queue<Nod> p_q;
 55     temp.dis = 0;
 56     temp.u = s;
 57     temp.num = weight[s];
 58     p_q.push(temp);
 59     while(!p_q.empty())
 60     {
 61         temp = p_q.top();
 62         p_q.pop();
 63         if(visit[temp.u])  continue;
 64         visit[temp.u] = 1;
 65         for(i=head[temp.u];i!=-1;i=edge[i].next)
 66         {
 67             v = edge[i].to;
 68             if(!visit[v])
 69             {
 70                 if(dis[v]>dis[temp.u]+edge[i].w||(dis[v]!=INF&&dis[v]==dis[temp.u]+edge[i].w&&ans[v]<ans[temp.u]+weight[v]))
 71                 {
 72                     dis[v] = dis[temp.u]+edge[i].w;
 73                     ans[v] = ans[temp.u]+weight[v];
 74                     now.u = v;
 75                     now.dis = dis[v];
 76                     now.num = ans[v];
 77 
 78                     p_q.push(now);
 79                 }
 80             }
 81         }
 82     }
 83 }
 84 
 85 int main()
 86 {
 87     int m,n;
 88     while(~scanf("%d%d",&n,&m))
 89     {
 90         int i;
 91         for(i=0;i<n;i++)    scanf("%d",weight+i);
 92         int id = 0;
 93         init(n);
 94         int a,b,c;
 95         for(i=0;i<m;i++)
 96         {
 97             scanf("%d%d%d",&a,&b,&c);
 98             a--,b--;
 99             edge[id].to = b;
100             edge[id].w = c;
101             edge[id].next = head[a];
102             head[a] = id++;  //将边 a --> b保存
103 
104             edge[id].to = a;
105             edge[id].w = c;
106             edge[id].next = head[b];
107             head[b] = id++;  //将边 b --> a保存
108         }
109         Dijkstra(0);
110         printf("%d\n",ans[n-1]);
111     }
112     return 0;
113 }

 

posted @ 2013-10-02 17:13  crazy_apple  阅读(236)  评论(0编辑  收藏  举报